C# .NET - Data Grid view with CHECKBOX column

Asked By Blair Yumi
01-Dec-10 01:47 AM
Hi, can some on help me with my problem?

How can I add a checkbox column programmatically and making it as a first column.

here is my code, but the checkbox is at the last column.

private void Form1_Load(object sender, EventArgs e)
      {
        string stat = "";
        int ave = 0;
        dataGridView1.ColumnCount = 5;


        DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
        dataGridView1.Columns.Add(chk);
        chk.HeaderText = "Check Data";
        chk.Name = "chk";

        dataGridView1.Columns[1].Name = "Substation/Office";
        dataGridView1.Columns[2].Name = "IP Address";
        dataGridView1.Columns[3].Name = "Status";
        dataGridView1.Columns[4].Name = "Average";

        string[] row = new string[] { "ce", "1", stat};
        dataGridView1.Rows.Add(row);
        row = new string[] { " Da", "1.1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "s", "19", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "cat", "1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "te", "6.1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "ffice", ".1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "ca", "51.1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "ban", "1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "Office", "1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "or", "19", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "Sur", "19", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "C", "3.1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "Norte", "1", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "Sto", "10", stat };
        dataGridView1.Rows.Add(row);
        row = new string[] { "mail", "1", stat };
        dataGridView1.Rows.Add(row);
      }



the checkbox column always appear at the last column of the grid view.

Thank You :)
  Shailendrasinh Parmar replied to Blair Yumi
01-Dec-10 01:53 AM
See the following article, it will help you to generate the checkbox column dynamically in Gridview

http://www.vbknowledgebase.com/?Id=90&Desc=Adding-checkbox-column-to-GridView-dynamically

Hope this helps.
  Reena Jain replied to Blair Yumi
01-Dec-10 01:53 AM
hi,

here is the code for you

DataGridViewCheckBoxColumn dataGridViewCheckBoxColumnObject = new DataGridViewCheckBoxColumn();
grid.Columns.Add(dataGridViewCheckBoxColumnObject);

or

private void CreatingCheckBox()
   {
   DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
   {
     column.Name = "column1";
     column.HeaderText = "CheckBox column";
     column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
     column.FlatStyle = FlatStyle.Standard;
     column.ThreeState = true;
     column.CellTemplate = new DataGridViewCheckBoxCell();
     column.CellTemplate.Style.BackColor = System.Drawing.Color.White;
   }
   dataGridView1.Columns.Insert(0, column); // 0 - is 1st column in a dgw
 
   //example of how to check (or uncheck boxes) - adding ticks.
   //every 2nd row will be checked, starting with 1st:
   for (int i = 0; i < dataGridView1.Rows.Count; i++)
   {
     if (i % 2 == 0)
     this.dataGridView1[0, i].Value = true;
     else
     this.dataGridView1[0, i].Value = false;
   }
   }

hope this will help you
  Rohan Dave replied to Blair Yumi
01-Dec-10 01:54 AM
do something like below

dataGridView1.Columns[0].Name = "chk"; // or your headertext

add your column at 0 th position..

that's all..
  Sagar P replied to Blair Yumi
01-Dec-10 01:58 AM
its simple instead of Columns.Add() use Insert like;

DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
//dataGridView1.Columns.Add(chk);

dataGridView1.Columns.Insert(0, chk); //Specify index of column as 0 so that it will insert this column as first column...

Check this link as well which will help you;

http://www.vbforums.com/showthread.php?t=610665

  Devil Scorpio replied to Blair Yumi
01-Dec-10 02:06 AM

Hi Mai,

Good Day

The columns appear in the order that their column styles were added to the tablestyle being used by the grid. If you want to change this order, you would need to create a new table style, and add the columnstyles in the order you want things to appear. Here is some code snippets that suggest how to do this in C#.

public void MoveColumn(DataGrid _dataGrid, string _mappingName, int fromCol, int toCol)
 
{
 
   if(fromCol == toCol) return;
 

 
   DataGridTableStyle oldTS = _dataGrid.TableStyles[_mappingName];
 
   DataGridTableStyle newTS = new DataGridTableStyle();
 
   newTS.MappingName = _mappingName;
 

 
   for(int i = 0; i < oldTS.GridColumnStyles.Count; ++i)
 
   {
 
      if(i != fromCol && fromCol < toCol)
 
         newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[i]);
 
      if(i == toCol)
 
         newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[fromCol]);
 
      if(i != fromCol && fromCol > toCol)
 
         newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[i]);   
 
   }
 

 
   _dataGrid.TableStyles.Remove(oldTS);
 
   _dataGrid.TableStyles.Add(newTS);
 
}
 

 
//sample usage
 
private void button1_Click(object sender, System.EventArgs e)
 
{
 
   MoveColumn(myDataGrid, "Customers", 3, 1);
 
}
 
Hope this helps :)

  Blair Yumi replied to Sagar P
01-Dec-10 02:35 AM
i think that helped but this is the scenario:

the first column should be a checkbox column and the latter columns accept string values,i can make the 1st column to appear first but i can't add the other columns,can someone help me with this problem? thanks a lot po...
  Blair Yumi replied to Reena Jain
01-Dec-10 02:36 AM
i think that helped but this is the scenario:

the first column should be a checkbox column and the latter columns accept string values,i can make the 1st column to appear first but i can't add the other columns,can someone help me with this problem? thanks a lot po...
  Sagar P replied to Blair Yumi
01-Dec-10 03:34 AM
If you have addded all other column as design time why dont you add your check box column also at a design time...

That will solve all problem i guess.... and which is simple and better way as well.... other wise add other columns also dynamically in your code....

check this link to add check box column in datagridview as design time;

http://www.dotnetspark.com/kb/151-add-checkbox-inside-datagridview-windows.aspx
Create New Account
help
Uninstall Parts of Office Office Office 2003 I am running short of disk space and rarely use Access, Publisher, or Power Excel. Add / Remove Programs has confusing wording. It has two choices. Change Features or Remove Office. I chose Remove Office figuring the next screen would ask which programs to remove. The next window just said some but not all Office programs? Thanks in advance. . . Bob Office Miscellaneous Discussions Windows XP (1 Office 2003 (1) Outlook (1) Office (1) Excel (1) Word (1) Powerpoint (1) Triangle (1) Change
error as : my queryText is as follows: queryText = SELECT \ "ACTIONS \ ", \ "TITLE \ ", \ "CONTENTCLASS \ ", \ "ISDOCUMENT \ ", \ "OWNEREMAIL \ ", \ "TEMPLATE \ ", \ "OFFICE \ ", \ "FLUENTLANGUAGES \ ", \ "PROJECTEXPERIENCE \ ", \ "FILEEXTENSION \ ", \ "PATH \ ", \ "HITHIGHLIGHTEDSUMMARY \ ", \ "AUTHOR \ ", \ "LASTMODIFIEDTIME \ ", \ "SIZE \ " FROM portal. . scope() WHERE WITH( \ "TITLE \ " ) AS #COLUMNGROUP ( \ "SCOPE \ " = 'CV' ) AND (CONTAINS(OFFICE, 'Africa') OR CONTAINS(OFFICE, 'Durban') OR CONTAINS(OFFICE, 'Johannesburg') OR CONTAINS OFFICE, 'Pietermaritzburg') OR CONTAINS(OFFICE, 'Asia') OR CONTAINS(OFFICE, 'Bangkok') OR CONTAINS(OFFICE, 'Beijing') OR CONTAINS(OFFICE, ' \ "Hong Kong \ "') OR CONTAINS(OFFICE, 'India') OR CONTAINS(OFFICE, 'Jakarta') OR
Is office.live, office.microsoft, workspace.office.live the same? Office Is office.live, office.microsoft, workspace.office.live the same or different programs? Office Miscellaneous Discussions Office System (1) Office (1) Soapboxare (1) Workspaces (1) Windows (1) Office.microsoft
can powerpoint be added to office? Office can powerpoint be purchased seprately, and be added to your existing small business office Office Miscellaneous Discussions MS Office (1) Office System (1) PowerPoint (1) Office 2007 (1) Office (1) Shortcuts (1) Howtobuy (1) Pagesfor (1) Yes you can. http: / / office.microsoft.com / en