Hi there,
Please help me. I am having problem trying to include Checkbox in 1st Column in DataGrid for all the Rows. Being a newbie and I am struggling doing it. I need your help.

Recommended Answers

All 11 Replies

Just Create OneTemplatecolumn like
<Columns>,in item template add one checkbox

Hi there,
Please help me. I am having problem trying to include Checkbox in 1st Column in DataGrid for all the Rows. Being a newbie and I am struggling doing it. I need your help.

You have to use tablestyles while binding the data to grid,
Create DataGridColumnStyle for each column.
for first column if u need check box follow this code.

DataGridTableStyle tblstyle = new DataGridTableStyle();
DataGridBoolColumn chkboxCol=new DataGridBoolColumn();
discontinuedCol.MappingName = "chkbox";
discontinuedCol.HeaderText = "";
discontinuedCol.Width = 30;
chkboxCol.AllowNull = false;
tblstyle .GridColumnStyles.Add(chkboxCol);

Hope it helps..............

Hi Vinnijain

C#.NET 2003 - Window Application.

I have tried out your suggestion and here is my overall script to add CheckBox into First Column and in every row in the DataGrid.

On compilation I got this error message:-The type or namespace name 'discontinuedCol' could not be found (are you missing a using directive or an assembly reference?)

Sample Script:-

private void FFormatDataGridColumn()
		
{
	DataGridTableStyle  DGTStyle = new DataGridTableStyle();
	DGTStyle.MappingName = "CustomerTbl";	 
	CheckBox chkbox = new CheckBox();
			 
	// 1st column checkbox
	DataGridBoolColumn chkboxCol = new DataGridBoolColumn();
	discontinuedCol.MappingName = "chkbox" ;
	discontinuedCol.HeaderText = "Select";
	discontinuedCol.Width = 30;
	discontinuedCol.AllowNull = false;	 

	//column 2
           DataGridColumnStyle  DGTcol2 = new DataGridTextBoxColumn();
	DGTcol2.MappingName = "CustID";
	DGTcol2.HeaderText  ="Customer ID";
	DGTcol2.Width =50;

	//column 3
            DataGridColumnStyle DGTcol3 = new DataGridTextBoxColumn();
	DGTcol3.MappingName = "FirstName";
	DGTcol3.HeaderText = "First NameAA";
	DGTcol3.Width =150;

	//column 4
           DataGridColumnStyle DGTcol4 = new DataGridTextBoxColumn();
	DGTcol4.MappingName = "LastName";
	DGTcol4.HeaderText = "Last NameBB";
	DGTcol4.Width = 150;						

	//add column to DGTStyle
	DGTStyle.GridColumnStyles.Add(chkboxCol);
	DGTStyle.GridColumnStyles.Add(DGTcol2);
	DGTStyle.GridColumnStyles.Add(DGTcol3);	
	DGTStyle.GridColumnStyles.Add(DGTcol4);	
	 
			 
	dataGrid1.TableStyles.Add(DGTStyle);
		 
}

Add the following line in your code:

DataGridBoolColumn discontinuedCol=new DataGridBoolColumn();

Hi Vinnijian,
I have change the script as per below based on your suggestion. The DataGrid does displays the row of data but it's without the CheckBox.
Not sure why it's not working.
Please Help. Thanks

CheckBox chkbox = new CheckBox();

DataGridBoolColumn discontinuedCol = new DataGridBoolColumn();
discontinuedCol.MappingName = "chkbox ";
discontinuedCol.HeaderText = "Select";
discontinuedCol.Width = 30;
discontinuedCol.AllowNull = false;

//add column to DGTStyle
DGTStyle.GridColumnStyles.Add(discontinuedCol);		 
DGTStyle.GridColumnStyles.Add(DGTcol2);	
DGTStyle.GridColumnStyles.Add(DGTcol3);	
DGTStyle.GridColumnStyles.Add(DGTcol4);
			 
dataGrid1.TableStyles.Add(DGTStyle);

Hi Vinnijian,

I did have a look at the URL site with VB format script and compared it with my C# format script. Logically it looks very similiar. The VB script end result shows the DataGrid with CheckBox but not my version. I am very puzzled.

HELP.........PLEASE HELP.........

From the property window of DataGridView Select the Columns Property.
Click on the (Collection) button .
From this window click on "Add" button.
select "Type" as "GridViewCheckboxColumn"

Hi Vinnijian,
DateGrid is only in C#.Net 2003 while DataGridView is in C#.Net 2005.

In C#Net, the DataGrid property does not have item COLUMN.

Not all the scripts in C#.Net 2005 is applicable in C#.Net 2003. Please help me with script for C#.Net 2003. Thank you very much.

Hi Vinnijian and All Helpers
Yeee...Haa.......Finally I got the script working. Thanks to you guys for sharing your knowledge with me. You guys and this FORUM are awesome

I would like to post the script here to share with newbie like me facing the same problems:

private void FLoadDataGrid()  
{
try
  {
     string strSql = "Select   FirstName, LastName from TblCustomer ";  
     sqlConn = new SqlConnection(connStr);
     sqlConn.Open();
    DA = new SqlDataAdapter(strSql, sqlConn);
    DS =   new DataSet("DS");

    //Clear dataset before refill 
   DS.Clear();
   DS.CaseSensitive = true;
   DA.Fill(DS,"PatientTbl");

   //declare datatable
   DataTable DT = new DataTable();
   DT.Clear();
   DT = DS.Tables[0];
            
    DataColumn colNewDGCol = new DataColumn();
   colNewDGCol.DataType = System.Type.GetType("System.Boolean");
   colNewDGCol.DefaultValue = false;
   colNewDGCol.ColumnName = ("Selected");
   DT.Columns.Add(colNewDGCol);
       
   //... file datagrid
   FFormatDataGridColumn();
   dataGrid1.DataSource = DT; 
 }
catch (Exception Ex)
   {
       MessageBox.show(Ex.Message);
    }
finally
   {
      sqlConn.Close();
   }
 }

/* ------------------------------------------------------------------ */

private void FFormatDataGridColumn()
 //define column structure for datagrid
{
   // declare tablestyle
  DataGridTableStyle DGStyle;  
  DGStyle = new DataGridTableStyle();  
  DGStyle.MappingName = "PatientTbl";   

  //checkbox column
  DataGridBoolColumn chkboxCol = new DataGridBoolColumn();
  chkboxCol.MappingName = "Selected";
  chkboxCol.HeaderText = "";
  chkboxCol.Width = 50;
  DGStyle.GridColumnStyles.Add(chkboxCol);

 //Firstname textbox column
    DataGridColumnStyle colFName = new DataGridTextBoxColumn();
   colFName.MappingName = "FirstName";
   colFName.HeaderText = "First Name";
   colFName.Width = 150;
   DGStyle.GridColumnStyles.Add(colFName);

  //lastName textbox column
    DataGridColumnStyle colLName = new DataGridTextBoxColumn();
   colLName.MappingName = "LastName";
   colLName.HeaderText = "Last Name";
   colLName.Width = 150;
  DGStyle.GridColumnStyles.Add(colLName);

// add table style to the dataGrid
   this.dataGrid1.TableStyles.Add(DGStyle);    
}


THANKS TO ALL OF YOU.

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.