C#Net2003 - Add CheckBox in DataGrid for all rows in Windows based Application and not ASP.NET
- Hi there,
Could someone please share with me sample script in C#Net 2003 for adding CheckBox in the First Column of all the DataGrid row in the C#Net 2003 Window Based Application.
Thanks.
Answers
- Hi Tee,
You can try something like this, suppose your data source is a DataTable.
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType= System.Type.GetType("System.Boolean");
myDataColumn.ColumnName = "Selected";
myDataTable.Columns.Add(myDataColumn);
Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer byTee Leong Tuesday, November 10, 2009 3:38 AM
Hi Zhi-Xin,
Nee How.....Thank you very much for sharing your knowledge me. You latest suggestion is working and I like to posted the script here to share with all new C#.Net 2003 members to help them.
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);
}
Thank you very much.
Here is my email address: Lennie_Kuah@xtra.co.nz / Lennie_Kuah@hotmail.com.
Hope to keep in contact with you. You are so helpful.
Have a Good day,
Cheers,
Tee Leong- Marked As Answer byTee Leong Tuesday, November 10, 2009 3:49 AM
All Replies
- Hi Tee,
You can use a DataGridBoolColumn,
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridboolcolumn.aspx
BTW, this forum is for WPF questions, for Windows Forms questions, please ask in http://social.msdn.microsoft.com/Forums/en-US/category/windowsforms
Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! Hi Zhi-Xin,
I have tried using DataGridBoolColumn in C#.Net 2003 Window Application but it's not working because chkboxCol.Mapping need to use the field from the table. What I am trying to do is to add a column into the DataGrid and then add CheckBox into it for all the rows.
Here is the script that I used and not working/
{
DataGridBoolColumn chkboxCol = new DataGridBoolColumn();
chkboxCol.MappingName = " ";
chkboxCol.Width = 150;
DGTStyle.GridColumnStyles.Add(chkboxCol);
dataGrid1.TableStyles["PatientTbl"].GridColumnStyles.Add(chkboxCol);
}
I need your help and Thanks for your contribution.
Nee How....
Cheers,
Tee Leong.- Hi Tee,
You can add a boolean field in the data source.
Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! Hi Zhi-Xin,
Can you please share with me the script to add a boolean field in the data source. I really having problem trying to do it. Being new to C#.NET 2003.
Cheers,
Tee Leong- Hi Tee,
You can try something like this, suppose your data source is a DataTable.
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType= System.Type.GetType("System.Boolean");
myDataColumn.ColumnName = "Selected";
myDataTable.Columns.Add(myDataColumn);
Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer byTee Leong Tuesday, November 10, 2009 3:38 AM
Hi Zhi-Xin,
Nee How.....Thank you very much for sharing your knowledge me. You latest suggestion is working and I like to posted the script here to share with all new C#.Net 2003 members to help them.
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);
}
Thank you very much.
Here is my email address: Lennie_Kuah@xtra.co.nz / Lennie_Kuah@hotmail.com.
Hope to keep in contact with you. You are so helpful.
Have a Good day,
Cheers,
Tee Leong- Marked As Answer byTee Leong Tuesday, November 10, 2009 3:49 AM


