Search code examples
c#sql.netdatagridviewcoalesce

Coalesce bit value with result appearing as a Checkbox in .NET Datagridview


I have a pre-existing query that I am modifying. The resultant data is pushed directly into a DataGridView object as a DataSet. There are multiple bit fields which result in checkboxes in the datagridview element. I want to mimic this functionality, but I need to use a Coalesce on my particular field. SQL is listed below:

SELECT res_id
        , res_resolution [Resolution]
        , res_type [Res. Type]
        , res_is_refund [Refund]
        , res_ship_replacement_today [Ship Today]
        , res_ship_replacement_after_return [Ship After Return]
        , res_return_required [Ret. Req.]
        , res_test [Test]
        , res_menu_divider [Menu Div.]
        , res_orderby [Menu Order]
        , res_new_return [New Returns]
        , res_process_return [Receive Returns]
        , res_modified [Last Updated]
        , res_active [Active]
        , COALESCE(rra_is_mispull_error, 0) [Mispull]
    FROM   jfi_return_resolution
          LEFT JOIN jfi_return_resolution_attribute rra ON rra.rra_res_id = res_id
    ORDER BY res_orderby;

The result of the above query is functionally correct, but the coalesce appears as a 1 or a 0. Since this is client facing, I need to make this a checkbox. Other fields (such as res_is_refund) appear in the DataGridView properly as a checkbox.

I've attempted to change the coalesce function to COALESCE(rra_is_mispull_error,false) [Mispull] but that just crashes the application. Removing the COALESCE all together would result in a bunch of nulls which would be as bad as 1's and 0's.

The C# that transfers the data into the DataGridView is in this form (as pseudocode because it isn't in a form which would easily be transferable to a forum post)

SqlDataAdapter SDA = new SqlDataAdapter();
DataSet ds = new DataSet();

Using(SqlConnection Conn ...)
{
    conn.Open();
    using(SqlCommand cmd = new SqlCommand(sql,conn))
    {
        SDA.SelectCommand = cmd;
        SDA.Fill(ds);
    }
}

MyDGV.DataSource = ds.Tables[0].DefaultView;

You can presume that the C# code and SQL actually functions properly outside of the Coalesce issue.


Solution

  • This should do the trick:

    SELECT res_id
        , res_resolution [Resolution]
        , res_type [Res. Type]
        , res_is_refund [Refund]
        , res_ship_replacement_today [Ship Today]
        , res_ship_replacement_after_return [Ship After Return]
        , res_return_required [Ret. Req.]
        , res_test [Test]
        , res_menu_divider [Menu Div.]
        , res_orderby [Menu Order]
        , res_new_return [New Returns]
        , res_process_return [Receive Returns]
        , res_modified [Last Updated]
        , res_active [Active]
        , CASE
             WHEN ISNULL(rra_is_mispull_error, 0) = 0 THEN 'false'
             ELSE 'true'
          END AS [Mispull]
    FROM   jfi_return_resolution
          LEFT JOIN jfi_return_resolution_attribute rra ON rra.rra_res_id = res_id
    ORDER BY res_orderby;
    

    if your data in column rra_is_mispull_error is of the type INT and you only have the following range [0-1] then you can do this:

    SELECT res_id
        , res_resolution [Resolution]
        , res_type [Res. Type]
        , res_is_refund [Refund]
        , res_ship_replacement_today [Ship Today]
        , res_ship_replacement_after_return [Ship After Return]
        , res_return_required [Ret. Req.]
        , res_test [Test]
        , res_menu_divider [Menu Div.]
        , res_orderby [Menu Order]
        , res_new_return [New Returns]
        , res_process_return [Receive Returns]
        , res_modified [Last Updated]
        , res_active [Active]
        , CAST(ISNULL(rra_is_mispull_error, 0) AS BIT) AS [Mispull]
    FROM   jfi_return_resolution
          LEFT JOIN jfi_return_resolution_attribute rra ON rra.rra_res_id = res_id
    ORDER BY res_orderby;