hi there,

how can i check the checkboxcolumn in a gatagrid view in C#.
thanxxx

I am not sure what you want?

can you give me a screen shot?

I am not sure what you want?

can you give me a screen shot?

the Done checkbox column in the datagrid view , is the one i am talking about.

is it like this, the code is below

public void AddToAction(String tno,String phase)
        {

            String query = @"Select TActionDes,TAssignTo,TDueDate,TStatus,TDateComplete,TComment from TopicAction Where TopicNo=@topicno and Phase=@phase ";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            command.Parameters.Add("@topicno", SqlDbType.VarChar).Value = tno;
            command.Parameters.Add("@phase", SqlDbType.Int).Value = phase;
            db10.openConnection();

           // DataGridViewCheckBoxColumn checkbox = (DataGridViewCheckBoxColumn)dgvDAction.Rows[0].Cells[3].OwningColumn;
            
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
           
            da.Fill(dt);

            for (int r = 0; r < dt.Rows.Count; r++)
            {

               [B] if (dt.Rows[r]["TStatus"].ToString() == "True")
                    dgvDAction.CurrentRow.Cells["Column3"].Value = "Vale is true";[/B]

                dgvDAction.Rows[r].Cells[0].Value = dt.Rows[r]["TActionDes"].ToString();
                dgvDAction.Rows[r].Cells[1].Value = dt.Rows[r]["TAssignTo"].ToString();
                dgvDAction.Rows[r].Cells[2].Value = dt.Rows[r]["TDueDate"].ToString();
                ////dgvDAction.Rows[r].Cells[3].Value = dt.Rows[r]["TStatus"].ToString();
                dgvDAction.Rows[r].Cells[4].Value = dt.Rows[r]["TDateComplete"].ToString();
                dgvDAction.Rows[r].Cells[5].Value = dt.Rows[r]["TComment"].ToString();
           
           }
           db10.closeConnection();

        }

in the code where it is boled and where the text "Vale is true" is i need to add CheckStatus.Checked.
how can i do this

this is not working

the value is saved as bit, I think you have to compare with 1 and 0
1 being true and 0 being false.

try

MessageBox.Show(dt.Rows[r]["TStatus"].ToString());

right before the if.

the value is saved as bit, I think you have to compare with 1 and 0
1 being true and 0 being false.

try

MessageBox.Show(dt.Rows[r]["TStatus"].ToString());

right before the if.

i know to compare the thing is how can i check the datagridview check box column depending on a condition?????????

i know to compare the thing is how can i check the datagridview check box column depending on a condition?????????

Not sure what you mean.

Not sure what you mean.

is the below code correct:

if (dt.Rows[r]["TStatus"].ToString() == "True")
                    dgvDAction.CurrentRow.Cells["Column3"].Value = CheckStatus.Checked;

#
if (dt.Rows[r]["TStatus"].ToString() == "1")

#
if (dt.Rows[r]["TStatus"].ToString() == "1")

i am not concern with the 1st line of code, i am concern about the 2nd line of code,
is that line correct?????????

>how can i check the checkboxcolumn in a gatagrid view in C#.

Binding (Bounded Columns)

//Click to checked/unchecked
       private void button1_Click(object sender, EventArgs e)
        {
            dt.Rows[0]["Status"] = !((bool)dt.Rows[0]["Status"]);
        }

        DataTable dt = new DataTable();
        DataView dv;
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("No",typeof(int));
            dt.Columns.Add("Status",typeof(bool));

            dt.Rows.Add(1, true);
            dt.Rows.Add(2, false);
            dt.Rows.Add(3, false);

            dv = dt.DefaultView;

            dataGridView1.DataSource = dv;
        }

>how can i check the checkboxcolumn in a gatagrid view in C#.

Binding (Bounded Columns)

//Click to checked/unchecked
       private void button1_Click(object sender, EventArgs e)
        {
            dt.Rows[0]["Status"] = !((bool)dt.Rows[0]["Status"]);
        }

        DataTable dt = new DataTable();
        DataView dv;
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("No",typeof(int));
            dt.Columns.Add("Status",typeof(bool));

            dt.Rows.Add(1, true);
            dt.Rows.Add(2, false);
            dt.Rows.Add(3, false);

            dv = dt.DefaultView;

            dataGridView1.DataSource = dv;
        }

the fourth row in your code gives an Invalid Cast Exception why is that
??

thanxx

>the fourth row in your code gives an Invalid Cast Exception why is that??

There is no data at specified cell/row. Please post your code.

>the fourth row in your code gives an Invalid Cast Exception why is that??

There is no data at specified cell/row. Please post your code.

public void AddToAction(String tno,String phase)
        {

            String query = @"Select TActionDes,TAssignTo,TDueDate,TStatus,TDateComplete,TComment from TopicAction Where TopicNo=@topicno and Phase=@phase ";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            command.Parameters.Add("@topicno", SqlDbType.VarChar).Value = tno;
            command.Parameters.Add("@phase", SqlDbType.Int).Value = phase;
            db10.openConnection();

           // DataGridViewCheckBoxColumn checkbox = (DataGridViewCheckBoxColumn)dgvDAction.Rows[0].Cells[3].OwningColumn;
            
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
           
            da.Fill(dt);

            for (int r = 0; r < dt.Rows.Count; r++)
            {

               dt.Rows[0]["TStatus"] = !((bool)dt.Rows[0]["TStatus"]);
               

                dgvDAction.Rows[r].Cells[0].Value = dt.Rows[r]["TActionDes"].ToString();
                dgvDAction.Rows[r].Cells[1].Value = dt.Rows[r]["TAssignTo"].ToString();
                dgvDAction.Rows[r].Cells[2].Value = dt.Rows[r]["TDueDate"].ToString();
                ////dgvDAction.Rows[r].Cells[3].Value = dt.Rows[r]["TStatus"].ToString();
                dgvDAction.Rows[r].Cells[4].Value = dt.Rows[r]["TDateComplete"].ToString();
                dgvDAction.Rows[r].Cells[5].Value = dt.Rows[r]["TComment"].ToString();
           
           }
           db10.closeConnection();

        }

this is the code,
the TStatus is the datagrid view checkbox column how can i tick it from the code.

public void AddToAction(String tno,String phase)
        {

            String query = @"Select TActionDes,TAssignTo,TDueDate,TStatus,TDateComplete,TComment from TopicAction Where TopicNo=@topicno and Phase=@phase ";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            command.Parameters.Add("@topicno", SqlDbType.VarChar).Value = tno;
            command.Parameters.Add("@phase", SqlDbType.Int).Value = phase;
           
           // DataGridViewCheckBoxColumn checkbox = (DataGridViewCheckBoxColumn)dgvDAction.Rows[0].Cells[3].OwningColumn;
            
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
           
            da.Fill(dt);

           
            dgvDAction.DataSource=dt;

        }
public void AddToAction(String tno,String phase)
        {

            String query = @"Select TActionDes,TAssignTo,TDueDate,TStatus,TDateComplete,TComment from TopicAction Where TopicNo=@topicno and Phase=@phase ";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            command.Parameters.Add("@topicno", SqlDbType.VarChar).Value = tno;
            command.Parameters.Add("@phase", SqlDbType.Int).Value = phase;
           
           // DataGridViewCheckBoxColumn checkbox = (DataGridViewCheckBoxColumn)dgvDAction.Rows[0].Cells[3].OwningColumn;
            
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
           
            da.Fill(dt);

           
            dgvDAction.DataSource=dt;

        }

i am using the unbound way
do u know to do it in that way????
so my columns are fixed in the datagrid view???

for (int r = 0; r < dt.Rows.Count; r++)
{
 
   ......
   dgvDAction.Rows[r].Cells[3].Value = dt.Rows[r]["TStatus"]; // I presume that the TStatus is bool type
   ....
}

This is easy. When you create your checkbox cell/column, you set the True/False values in your declaration. You can set it to whatever you want. Then, you can just have the cell equal that value.

checkBoxColumn.TrueValue = true; //as bool
            checkBoxColumn.FalseValue = false; //as bool

foreach (DataGridViewRow dr in dataGridView1.Rows)
            {

                dr.Cells[0].Value = true; //sets them to checked
            }

You can set it to whatever you want.

checkBoxColumn.TrueValue = "foo"; //as a string
            checkBoxColumn.FalseValue = "oof"; //as a string

foreach (DataGridViewRow dr in dataGridView1.Rows)
            {

                dr.Cells[0].Value = "foo"; //sets them to checked
            }
for (int r = 0; r < dt.Rows.Count; r++)
{
 
   ......
   dgvDAction.Rows[r].Cells[3].Value = dt.Rows[r]["TStatus"]; // I presume that the TStatus is bool type
   ....
}

no it is a string value, i get the value of it from the database and if it is true i am trying to check the check box, i am trying to this,

but how can i do it with unbounded datagrid view?????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.