Click here to Skip to main content
12,944,475 members (52,226 online)
Rate this:
 
Please Sign up or sign in to vote.
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 17-Nov-12 19:15pm
Updated 17-Nov-12 19:37pm
v2
Comments
Dominic Abraham 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 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 ?
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 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
Top Experts
Last 24hrsThis month
OriginalGriff 4,653
CHill60 2,970
Maciej Los 2,388
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2017
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