How to Bind ComboBox in Grid to some other table?

  • Thread starter Ahmad Jalil Qarshi
  • Start date
A

Ahmad Jalil Qarshi

Hi,

I am developing a Windows Form based application in C# 2005. I have placed a
Grid control on the Form and using MS SQL server 2000 at back end. Now the
Grid is showing different columns from a table Table1.

There is a column in the grid named "Sender Name". A combobox is embeded in
this column.

Now what I want is that when a row of the Grid is in edit mode, this
combobox must fetch "Sender Name" from another table Table2. I will select a
specific entry from the combobox and then update the whole row in Table1.

Is it possible. If yes then how?

Any help will be highly appriciated.

Thanks in anticipation.

Ahmad Jalil Qarhsi
 
C

ClayB

Here is a Form.Load that sets up a combobox column in a DataGridView
that is using another table as a foriegn key lookup.

private void Form1_Load(object sender, EventArgs e)
{

#region Get the DataTables
DataTable dt = new DataTable("MyTable");

int nCols = 4;
int nRows = 20;
Random r = new Random(123345345);

for (int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}", i),
typeof(int)));

for (int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < nCols; j++)
dr[j] = r.Next(5).ToString();
dt.Rows.Add(dr);
}

DataTable dt1 = new DataTable("ComboTable");

dt1.Columns.Add(new DataColumn("id", typeof(int)));
dt1.Columns.Add(new DataColumn("name"));


for (int i = 0; i < 5; ++i)
{
DataRow dr = dt1.NewRow();
dr[0] = i;
dr[1] = "Name" + i.ToString();
dt1.Rows.Add(dr);
}
#endregion

this.dataGridView1.DataSource = dt;

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = dt1;
col.DisplayMember = "Name";
col.ValueMember = "id";
col.Name = "combo";
col.DataPropertyName = "Col0"; //mappingname

this.dataGridView1.Columns.Remove("Col0");
this.dataGridView1.Columns.Insert(0, col);
}
======================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top