471,319 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

C# DataGridView: How to map defined columns to DataTable columns ??

Ok, stumped one more time, I'm trying to learn how to use a DataGridView in
place of the old DataGrid control.
QUESTION: How do I map the columns returned from a RunTime sql query to the
columns defined (defined columns collection) in a DataGridView??

i.e.
sql_query.display_name = dataGridView1.dg1vc_display_name
sql_query.name_last = dataGridView1.name_last
sql_query.name_first = dataGridView1.name_first
sql_query.name_mi = dataGridView1.name_mi
sql_query. name_company = dataGridView1.name_company
sql_query. name_other = dataGridView1.name_other
sql_query. street1 = dataGridView1.street1
sql_query.city = dataGridView1.city
sql_query.status = dataGridView1.status
sql_query.last_updt_date = dataGridView1.last_updt_date

This was easy to do with the old DataGrid control by setting a table style
and mapping the table style columns to the DataTable columns. I don't see
any way to do that with the DataGridView.

I've defined the following
private System.Windows.Forms.DataGridView dataGridView1;

private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_display_name;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_last_updt_date;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_name_last;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_name_first;
private System.Windows.Forms.DataGridViewTextBoxColumn dgv1_name_mi;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_name_company;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_name_other;
private System.Windows.Forms.DataGridViewTextBoxColumn dgv1_street1;
private System.Windows.Forms.DataGridViewTextBoxColumn dgv1_city;
private System.Windows.Forms.DataGridViewTextBoxColumn
dgv1_entity_status;

In my code I submit the following query generates a DataTable within a
DataSet object.

private void cmdSearch_Click(object sender, EventArgs e)
{

SELECT
dir_id,
dbo.return_display_name(NULL,NULL,directory_id,'LF M',1,1,0,NULL) as
display_name,
name_last,
name_first,
name_mi,
name_company,
name_other,
street1,
city,
status,
last_updt_date
FROM
directory
WHERE
name_last = '" + this.txtLastName.Text.ToUpper().ToString() + "' AND
(status IS NULL OR status = 'AC')
ORDER BY
name_last,
name_first,
name_mi,
name_company,
last_updt_date DESC

// The results of the query above are returned into
oDs.Tables["SearchResults"];
// This query returns about 40 rows.

this.bindingSource1.DataSource = oDs.Tables["SearchResults"];

this.dataGridView1.DataSource = null;
this.dataGridView1.DataBindings.Clear();
this.dataGridView1.DataSource = this.bindingSource1;

}

With "dataGridView1.AutoGenerateColumns = true" the query columns are
appended to the defined dataGridView1columns.
With "dataGridView1.AutoGenerateColumns = false" only the blank defined
dataGridView1columns appear.

Any help getting this working would be greatly appreciated !!!
Jun 21 '07 #1
0 6127

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ctk_at | last post: by
3 posts views Thread by Art | last post: by
7 posts views Thread by Mitchell S. Honnert | last post: by
6 posts views Thread by =?Utf-8?B?TWFyY2Vsbw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.