472,965 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 6201

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ctk_at | last post by:
Hi, has anyone probably solved the following issue: I want to create a datagridview with a combobox displaying value from a datatable. This work fine as long as I 'hardcode' the necessary...
8
by: George | last post by:
Hi, I have been trying to see if I can do the following: 1. Create a DataGridView 2. Create 2 columns in the DataGridView (1 textbox and the other combobox) 3. Create a DataTable containing...
3
by: Art | last post by:
Hi, Forgive me for posting this message in 2 groups -- it appears that the Data Access group isn't very active, so I'm posting again here: I have a DataTable that I want to fill using a...
3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
3
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my...
6
by: =?Utf-8?B?TWFyY2Vsbw==?= | last post by:
Hello, I am new to pulling data into VB and using DataGridViews..... so this might be a dumb question but I can not seem to understand how to make this work. What I need acomplished: Exec a...
6
by: JoaquimC | last post by:
Helo, I got this problem with a c# DataGridView in VS2005: The DataGridView has 2 columns: colArtigoGenerico, colArtigoFinal; and this columns are populated with data from 2 columns from a...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.