473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 6223

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.