在datagrid c#.net winforms中将列表绑定到用户控件(其中包含按钮的文本框) [英] Bind list to user control (textbox having button within it) in datagrid c# .net winforms

查看:54
本文介绍了在datagrid c#.net winforms中将列表绑定到用户控件(其中包含按钮的文本框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在winforms中定制了数据网格。每个单元格都使用用户控件进行修改(文本框中包含按钮)。我想将列表值分配给datagrid。怎么办?



这里我不能使用

Hi,

I have customized data grid in winforms. Each cell is modified with user control (textbox having button within it). I want to assign list values to datagrid. How to do?

Here I can't use

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;

or

dataGridView.DataSource = List;







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单元格。这里的问题是在gridview单元格中嵌入了文本框和按钮。



任何帮助表示感谢。





问候,

Chandra




Because it will not reflect in textbox of the datagrid cell. The problem here is with textbox and button embedded within gridview cell.

Any help appreciated.


Regards,
Chandra

推荐答案

foreach (var item in List)
{
    dataGridView1.Rows.Add(item);
}





以上是数据网格视图中1列问题的解决方案(示例)。





Above is solution (sample) for the problem for 1 column in datagrid view.

foreach (var item in List)
{
    string temp = "Get 2nd variable value";
    dataGridView1.Rows.Add(item, temp);
}





以上是数据网格视图中2列的解决方案(示例)。



感谢您的支持。



Above is the solution (sample) for the 2 columns in datagrid view.

Thanks for all your support.


这篇关于在datagrid c#.net winforms中将列表绑定到用户控件(其中包含按钮的文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆