Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to set the value of one column of gridview to true and this column type is checkedbox how can i do this"
"
(dataGridView2.Columns[12] as DataGridViewCheckBoxColumn).value = true;

how can i do this
Posted
Updated 17-Nov-12 19:37pm
v2
Comments
Dominic Abraham 18-Nov-12 1:50am    
If the solution is helpful, mark it as answer.

Hi

Please try the below code

C#
foreach (DataGridViewRow dgRow in dataGridView1.Rows)
           {
               var cellValue = dgRow.Cells[12] as DataGridViewCheckBoxCell;
               cellValue.Value = true;
           }
 
Share this answer
 
Comments
f.sarikhani 18-Nov-12 2:15am    
this work true but i want to read data from data base and if a condition is true this checkbox is checked
Dominic Abraham 18-Nov-12 2:27am    
Can you post some lines of your code, so that i can correct you. What is the condition ? are you comparing the value from the database with a value of a column in gridview ?
C#
public DataTable Search(int m)
{
   DataTable k = new System.Data.DataTable();
   try
   {
      string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
      Qconnection.ConnectionString = str;
      Qcommand.Connection = Qconnection;
      string cond="";
              

      string commandText = "SELECT  foroosh.date, moshtari.name, moshtari.lname,moshtari.mobile,  foroosh.type, foroosh.model, foroosh.tip, foroosh.karkard,foroosh.color, foroosh.sales, foroosh.badane,foroosh.price,foroosh.mojood,foroosh.comment  FROM foroosh INNER JOIN moshtari ON foroosh.mid = moshtari.id where foroosh.parvande=@par "+cond ;
      Qcommand.Parameters.AddWithValue("@type", dataGridView2.Rows[m].Cells[0].Value.ToString());
      Qcommand.Parameters.AddWithValue("@modelfrom", dataGridView2.Rows[m].Cells[1].Value.ToString());
      Qcommand.Parameters.AddWithValue("@modelto", dataGridView2.Rows[m].Cells[2].Value.ToString());
      Qcommand.Parameters.AddWithValue("@tip", dataGridView2.Rows[m].Cells[3].Value.ToString());
      Qcommand.Parameters.AddWithValue("@karkardfrom", dataGridView2.Rows[m].Cells[4].Value.ToString());
      Qcommand.Parameters.AddWithValue("@karkardto", dataGridView2.Rows[m].Cells[5].Value.ToString());
      Qcommand.Parameters.AddWithValue("@color", dataGridView2.Rows[m].Cells[6].Value.ToString());
      Qcommand.Parameters.AddWithValue("@pricefrom", dataGridView2.Rows[m].Cells[7].Value.ToString());
      Qcommand.Parameters.AddWithValue("@priceto", dataGridView2.Rows[m].Cells[8].Value.ToString());
      Qcommand.Parameters.AddWithValue("@par", "faal");
      Qcommand.CommandText = commandText;
      Qcommand.CommandType = CommandType.Text;
      SqlCeDataAdapter a = new SqlCeDataAdapter();
      a.SelectCommand = Qcommand;
      a.Fill(k);
      Qconnection.Open();
      Qconnection.Close();
      return k;
   }
   catch (Exception ex)
   {
      throw new Exception(ex.Message);
      return k;
   }
}


i want to read from data base and if value of column mojood of table is true the chech box column of gridview checked
 
Share this answer
 
v2
Comments
Dominic Abraham 18-Nov-12 4:21am    
Are you expecting a single row or multiple row in this dataset 'k' "
As dominic has replied

foreach (DataGridViewRow dgRow in dataGridView1.Rows)
           {
               var cellValue = dgRow.Cells[12] as DataGridViewCheckBoxCell;
                //Check the Database value for true or false to make the checkbox checked or unchecked
               if(your Condition)
               cellValue.Value = true;
           }
 
Share this answer
 

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