如何在数据网格控件中每行的开头添加复选框控件?
我正在用 C# 开发移动应用程序。我想在数据网格控件中每行的开头动态添加复选框控件。基于该特定的复选框选择,我想触发该事件。我想添加复选框列和基于任何特定复选框的选择,我想在该复选框选定的行上触发事件。我正在使用以下代码。
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();
}
您能给我提供任何可以解决上述问题的代码或链接吗?
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 want 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个向数据网格添加复选框的示例:
http://www.codeproject.com/Articles/2652/Adding-a-CheckBox-column-to-your-DataGrid
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