Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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;


C#
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;
    }
}



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
Posted
Updated 7-Jan-16 18:55pm
v3

1 solution

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


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

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


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

Thanks for all your support.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900