Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17-Nov-12 19:15pm
Edited 17-Nov-12 19:37pm
v2
Comments
Dominic Abraham at 18-Nov-12 1:50am
   
If the solution is helpful, mark it as answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
Please try the below code
 
 foreach (DataGridViewRow dgRow in dataGridView1.Rows)
            {
                var cellValue = dgRow.Cells[12] as DataGridViewCheckBoxCell;
                cellValue.Value = true;
            }
  Permalink  
Comments
f.sarikhani at 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 at 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 ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Dominic Abraham at 18-Nov-12 4:21am
   
Are you expecting a single row or multiple row in this dataset 'k' "
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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;
            }
  Permalink  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 OriginalGriff 394
2 thatraja 370
3 Sergey Alexandrovich Kryukov 319
4 CPallini 179
0 OriginalGriff 697
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 495
3 thatraja 370
4 Sergey Alexandrovich Kryukov 344


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100