将列表绑定到datagrid c#.net winforms中的用户控件(文本框中有按钮)
我在winforms中定制了数据网格。每个单元格都使用用户控件进行修改(文本框中有按钮)。我想给datagrid分配列表值。怎么办 这里我不能用将列表绑定到datagrid c#.net winforms中的用户控件(文本框中有按钮),c#,.net,winforms,user-controls,C#,.net,Winforms,User Controls,我在winforms中定制了数据网格。每个单元格都使用用户控件进行修改(文本框中有按钮)。我想给datagrid分配列表值。怎么办 这里我不能用 DataTable dt = new DataTable(); dt.Columns.Add("Source", typeof(string)); foreach (var item in List) { DataRow dr = dt.NewRow(); dr[0] = item.toString(); dt.Rows.A
DataTable dt = new DataTable();
dt.Columns.Add("Source", typeof(string));
foreach (var item in List)
{
DataRow dr = dt.NewRow();
dr[0] = item.toString();
dt.Rows.Add(dr);
}
dataGridView.DataSource = dt;
或
因为它不会反映在datagrid单元格的文本框中。这里的问题是嵌入gridview单元格中的文本框和按钮
其他信息:
this.Source = new DataGridViewTextBoxColumn();
this.Source.Name = "Source";
this.Source.Width = 350;
this.dataGridView1.Columns.Add(Source);
this.txtbtnControl = new TextBoxButtonControl();
this.txtbtnControl.Visible = false;
this.dataGridView1.Controls.Add(this.txtbtnControl);
public class TextBoxButtonControl : UserControl
{
public TextBox txtCode;
public Button btnCode;
public TextBoxButtonControl()
{
this.txtCode = new TextBox();
this.Controls.Add(this.txtCode);
this.btnCode = new Button();
this.Controls.Add(this.btnCode);
this.renderControl();
}
public void renderControl()
{
this.txtCode.Location = new Point(0, 0);
this.txtCode.Width = this.Width + 115;
this.txtCode.Height = this.Height;
this.btnCode.Location = new Point(this.Width + 115, 0);
this.btnCode.Width = 32;
this.btnCode.Height = 21;
}
}
以上是datagrid视图中1列问题的解决方案(示例)
foreach (var item in List)
{
string temp = "Get 2nd variable value";
dataGridView1.Rows.Add(item, temp);
}
以上是datagrid视图中2列的解决方案(示例)
foreach (var item in List)
{
string temp = "Get 2nd variable value";
dataGridView1.Rows.Add(item, temp);
}
感谢您的支持。您可能只将用户控件置于单元格上方,而您应该创建一个新的
DataGridViewColumn
类型并使用它。您好,雷扎·阿盖伊,我更新了问题。我已经剪切了usercontrol并嵌入到datagrid的单元格中。正确的方法是创建一个自定义的DataGridViewColumn
。这样,您只需将控件的一个实例放置在单元格上方。你甚至没有将单元格的值传递给它。嗨,Reza Aghaei,我搜索过很多论坛,大多数论坛只提供这种实现。你能指导我解决这个问题吗?嗨,钱德拉,你可以看看这个。如果你想继续你的方式,你可以看看我的答案。
foreach (var item in List)
{
string temp = "Get 2nd variable value";
dataGridView1.Rows.Add(item, temp);
}