By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,462 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,462 IT Pros & Developers. It's quick & easy.

How to Bind ComboBox in Grid to some other table?

P: n/a
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
Feb 6 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Feb 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.