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

F

frostbb

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,'LFM',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 !!!
 

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