473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.displ ay_name = dataGridView1.d g1vc_display_na me
sql_query.name_ last = dataGridView1.n ame_last
sql_query.name_ first = dataGridView1.n ame_first
sql_query.name_ mi = dataGridView1.n ame_mi
sql_query. name_company = dataGridView1.n ame_company
sql_query. name_other = dataGridView1.n ame_other
sql_query. street1 = dataGridView1.s treet1
sql_query.city = dataGridView1.c ity
sql_query.statu s = dataGridView1.s tatus
sql_query.last_ updt_date = dataGridView1.l ast_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.DataGridV iew dataGridView1;

private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_display_na me;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_last_updt_ date;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_name_last;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_name_first ;
private System.Windows. Forms.DataGridV iewTextBoxColum n dgv1_name_mi;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_name_compa ny;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_name_other ;
private System.Windows. Forms.DataGridV iewTextBoxColum n dgv1_street1;
private System.Windows. Forms.DataGridV iewTextBoxColum n dgv1_city;
private System.Windows. Forms.DataGridV iewTextBoxColum n
dgv1_entity_sta tus;

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_disp lay_name(NULL,N ULL,directory_i d,'LFM',1,1,0,N ULL) 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.txtLastNam e.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["SearchResu lts"];
// This query returns about 40 rows.

this.bindingSou rce1.DataSource = oDs.Tables["SearchResu lts"];

this.dataGridVi ew1.DataSource = null;
this.dataGridVi ew1.DataBinding s.Clear();
this.dataGridVi ew1.DataSource = this.bindingSou rce1;

}

With "dataGridView1. AutoGenerateCol umns = true" the query columns are
appended to the defined dataGridView1co lumns.
With "dataGridView1. AutoGenerateCol umns = false" only the blank defined
dataGridView1co lumns appear.

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

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

Similar topics

2
1570
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 properties into the datagridcolumn. Is there any way to get the property information from a database which sort of "configures" the datagridcolumn. ie I do...
8
34216
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 my data. (AutoGenerateColumns is true). The data is 2 columns of System.String cell. 4. If I assign the DataTable to DataGridView.DataSource, I get...
3
917
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 DataGridView. I've set the DataSource of the DataGridView to my DataTable. My problem is that I want some of the columns to be drop downs. For example,...
3
32250
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 datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable...
7
12571
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 the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store...
3
94226
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 database. For example: table JobTypes 1 | Manager 2 | Controller 3 | Supervisor table Employee
6
2337
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 select statement and out put the results to a DataGridView. The columns must be created on the fly as the query may return different fields. A...
6
1551
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 DataTable: "Detalhes". 1st i populate the DataGrid with a Datatable using this code: string select = "SELECT a.CArtigoGenerico, a.CArtigoFinal " +...
1
3364
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: 1) export to excel button exports the visible columns from the datagridview to excel (this works fine)
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.