Convert field obtained from SQL Server to a checkbox of a datagridview in C#

wgeznvg7  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(101)

I'm showing data from my contacts table to a datagridview - all good so far. This is my code as I show the information by datasource.

This is my data_layer class:

public DataTable Contactos_ClieProvs(String basesita, String codClie)
{
    DataTable dt = new DataTable();

    try
    {
        SqlConnection cnn = new SqlConnection(conexion.CargarBase(basesita));

        SqlCommand cmd = new SqlCommand("execute CONTACTOS  @tipo=13,@IdClieProv='" + codClie + "'", cnn);
        // cmd.CommandTimeout = 0;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        try
        {
            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }
            else
            {
                cnn.Close();
                cnn.Open();
            }

            da.Fill(dt);
        }
        catch (SqlException ex) 
        { 
             throw ex; 
        }
        finally
        {
            cnn.Close();
        }
    }
    catch (SqlException ex1) 
    { 
        throw ex1; 
    }

    return dt;
}

This is code as shown towards the datagridview:

private void txtcod_TextChanged(object sender, EventArgs e)
{
    if ("" == "") 
        dt = negCli.DatosContacto(MODULOS.Globales.Base, txtcod.Text);
         
    bs.DataSource = dt;
    bs.AddingNew += bs_AddingNew;
    dt.RowDeleting += dt_RowDeleting;
    this.dgvcontactos.DataSource = bs;

    // this.dgvcontactos.DataSource = negCli.DatosContacto(MODULOS.Globales.Base, txtcod.Text);
    return;
}

Now I am looking for a way to convert this specific field "To charge" where if it is 1 the check is selected and if it is 0 it is not selected, I have tried to add it in a design way but I have not had good results, someone please can help since I am just beginning in this world.

g0czyy6m

g0czyy6m1#

You can simply cast the field in your stored procedure to be BOOLEAN (or BIT if you are using MS SQL) rather than numeric(1,0) and the field will "automatically" become a CheckBox type when you create/bind it. Here would be the MS SQL example:

SELECT CAST(DB.Field AS BIT) FROM DB

相关问题