How to add checkbox control at the beginning of every row in datagrid control?
I am developing mobile application in C#. I want to add the checkbox control dynamically at the beginning of every row in the datagrid control & based on that particular checkbox selection I want to fire the event. I want to add the checkbox column & based on the selection of any particular checkbox, I w开发者_开发技巧ant to fire the event on that checkbox selected row. I am using the following code.
private void ShowRegistrationKeyDetails_Load(object sender, EventArgs e)
{
GridHeight = 40;
SQLiteDataReader SQLiteDrKeyObj = null;
DataTable dt = new DataTable();
DataManager DataMgrObj = new DataManager();
//int KeyID = Cust_ID;
//string Client_Key1 = Client_Key;
int KeyID = Selected_Customer_ID;
//string Client_Key = Selected_Client_Key;
SQLiteDrKeyObj = DataMgrObj.getRegistrationKey(KeyID);
dt.Load(SQLiteDrKeyObj);
//dt.Columns.Add(new DataColumn("Select", typeof(Boolean)));
RegKeyInfodataGrid.DataSource = dt;
SizeColumns(RegKeyInfodataGrid);
RegKeyInfodataGrid.Height = GridHeight;
}
protected void SizeColumns(DataGrid grid)
{
//grid.Controls.Add(new CheckBox());
Graphics g = CreateGraphics();
DataTable dataTable = (DataTable)grid.DataSource;
//DataColumn dtcCheck = new DataColumn("IsMandatory");//create the data
////column object with the name
//dtcCheck.DataType = System.Type.GetType("System.Boolean");//Set its
////data Type
//dtcCheck.DefaultValue = false;//Set the default value
//dataTable.Columns.Add(dtcCheck);//Add the above column to the
//Data Table
//Set the Data Grid Source as the Data Table createed above
//grid.DataSource = dataTable1;
// set style property when first time the grid loads, next time onwards // it will maintain its property
DataGridTableStyle dataGridTableStyle = new DataGridTableStyle();
dataGridTableStyle.MappingName = dataTable.TableName;
int RowCount = dataTable.Rows.Count;
//foreach(
foreach (DataColumn dataColumn in dataTable.Columns)
{
int maxSize = 0;
SizeF size = g.MeasureString(
dataColumn.ColumnName,
grid.Font
);
if (size.Width > maxSize)
maxSize = (int)size.Width;
//grid.Controls.Add(new CheckBox());
foreach (DataRow row in dataTable.Rows)
{
size = g.MeasureString(
row[dataColumn.ColumnName].ToString(),
grid.Font
);
if (size.Width > maxSize)
maxSize = (int)size.Width;
// AutoResize DataGrid Control
string Act_Date = dataColumn.ColumnName;
if (Act_Date == "Activation_Date")
{
GridHeight = GridHeight + 17;
//CheckBox chk = new CheckBox();
//chk.Location = new Point(20, 30);
//this.Controls.Add(chk);
//dataTable.Rows.Add(new CheckBox());
}
}
DataGridColumnStyle dataGridColumnStyle = new DataGridTextBoxColumn();
dataGridColumnStyle.MappingName = dataColumn.ColumnName;
dataGridColumnStyle.HeaderText = dataColumn.ColumnName;
dataGridColumnStyle.Width = maxSize + 5;
dataGridTableStyle.GridColumnStyles.Add(dataGridColumnStyle);
}
grid.TableStyles.Add(dataGridTableStyle);
g.Dispose();
}
private void BackmenuItem_Click(object sender, EventArgs e)
{
QueryDetails QueryDetailsObj = new QueryDetails();
QueryDetailsObj.Show();
}
Can you please provide me any code or link through which I can resolve the above issue?
There is an example of adding a check box to a Datagrid:
http://www.codeproject.com/Articles/2652/Adding-a-CheckBox-column-to-your-DataGrid
精彩评论