Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I need to populate a table from the SQL db
into a datagridview. My issue is the last column
is check boxes. The column in the DB that has to
trigger these check boxes is filed with "1 and 0"

If its a zero the check box must be unchecked and
when its 1 it must be checked.

Please any help would be appriciated

This is the structure I use when populating the grid:

clsConnection sqlConn = new clsConnection();
            SqlCommand sqlComm = new SqlCommand();
            DataTable dt = new DataTable();

            sqlComm.Connection = sqlConn.connection;
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.CommandText = "ElectricityData.Schedules_LoadGrid";
            sqlComm.Parameters.Add("@MeterID", SqlDbType.Int).Value = iMeterID2;
            sqlComm.Parameters.Add("@ReadingType", SqlDbType.VarChar, 50).Value = cboReadingType2.Text.ToString().Trim();

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlComm;
            da.Fill(dt);
            dgvSchedules.ReadOnly = true;
            dgvSchedules.Rows.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvSchedules.Rows.Add(dt.Rows[i]["MeterNumber"].ToString(),
                                     dt.Rows[i]["Description"].ToString(),
                                     dt.Rows[i]["Type"].ToString(),
                                     dt.Rows[i]["DateTimeFrom"].ToString(),
                                     dt.Rows[i]["Enabled"].ToString());
            }
Posted

1 solution

try this:-
clsConnection sqlConn = new clsConnection();
            SqlCommand sqlComm = new SqlCommand();
            DataTable dt = new DataTable();
 
            sqlComm.Connection = sqlConn.connection;
            sqlComm.CommandType = CommandType.StoredProcedure;
            sqlComm.CommandText = "ElectricityData.Schedules_LoadGrid";
            sqlComm.Parameters.Add("@MeterID", SqlDbType.Int).Value = iMeterID2;
            sqlComm.Parameters.Add("@ReadingType", SqlDbType.VarChar, 50).Value = cboReadingType2.Text.ToString().Trim();
 
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlComm;
            da.Fill(dt);
            dgvSchedules.ReadOnly = true;
            dgvSchedules.Rows.Clear();
 
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvSchedules.Rows.Add();
                dgvSchedules.Rows[i].Cells["colMeterNumber"].Value=dt.Rows[i]["MeterNumber"].ToString();
                dgvSchedules.Rows[i].Cells["colDescription"].Value=dt.Rows[i]["Description"].ToString();
                dgvSchedules.Rows[i].Cells["colType"].Value=dt.Rows[i]["Type"].ToString();
                dgvSchedules.Rows[i].Cells["colDateTimeFrom"].Value=dt.Rows[i]["DateTimeFrom"].ToString();
                if(Convert.ToBoolean(dt.Rows[i]["Enabled"])==false)
                {
                 dgvSchedules.Rows[i].Cells["colEnabled"].Value=false;
                }
                else(Convert.ToBoolean(dt.Rows[i]["Enabled"])==true)
                {
                 dgvSchedules.Rows[i].Cells["colEnabled"].Value=true;
                }
            }


Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
v2
Comments
Ben Paxton 23-Feb-12 13:55pm    
GREAT! thanks alot
Varun Sareen 23-Feb-12 22:48pm    
your welcome dear

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900