Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# fill a datagrdview and text box from 2 tables

Posted on 2009-05-11
2
Medium Priority
?
331 Views
Last Modified: 2012-05-07
I have a counter that is retuned to a Main form and i would like to populate a datagridvierw and a text box resulting from this counter with results from 2 tables (Customer and contact tables), how can i do this? is it possible? Currently i have a view (vwCustomersandContacts that has customer names, address and the contacts for that customer) that returns me the records that is = to counter but its causing me a headache when trying to delete a row from the datagridview so i though if i use a table instead of a view would be simplier
using (frmSearchCustomer form = new frmSearchCustomer())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection();
                    conn = new SqlConnection(@"Data Source=<SQLInstance;Initial Catalog=<Database>;Integrated Security=True");
                    comm = new SqlCommand("SELECT * FROM vwCustomersandContacts WHERE CustomerCounter = " + form.Counter + "");
                    da = new SqlDataAdapter();
                    ds = new DataSet();
                    comm.Connection = conn;
                    da.SelectCommand = comm;
                    datagridview1.AutoGenerateColumns = false;
                    //Datagridview1 column1
                    var col1 = new DataGridViewColumn();
                    col1.DataPropertyName = "FirstName";
                    col1.Name = "First Name";
                    var cell1 = new DataGridViewTextBoxCell();
                    col1.CellTemplate = cell1;
                    datagridview1.Columns.Add(col1);
                    //Datagridview1 column2
                    var col2 = new DataGridViewColumn();
                    col2.DataPropertyName = "LastName";
                    col2.Name = "Last Name";
                    var cell2 = new DataGridViewTextBoxCell();
                    col2.CellTemplate = cell2;
                    datagridview1.Columns.Add(col2);
                    //Datagridview1 column3
                    var col3 = new DataGridViewColumn();
                    col3.DataPropertyName = "Telephone";
                    col3.Name = "Telephone";
                    var cell3 = new DataGridViewTextBoxCell();
                    col3.CellTemplate = cell3;
                    datagridview1.Columns.Add(col3);
                    //Datagridview1 column4
                    var col4 = new DataGridViewColumn();
                    col4.DataPropertyName = "Mobile";
                    col4.Name = "Mobile";
                    var cell4 = new DataGridViewTextBoxCell();
                    col4.CellTemplate = cell4;
                    datagridview1.Columns.Add(col4);
                    //Datagridview1 column5
                    var col5 = new DataGridViewColumn();
                    col5.DataPropertyName = "Department";
                    col5.Name = "Department";
                    var cell5 = new DataGridViewTextBoxCell();
                    col5.CellTemplate = cell5;
                    datagridview1.Columns.Add(col5);
                    //Datagridview1 column6
                    var col6 = new DataGridViewColumn();
                    col6.DataPropertyName = "Position";
                    col6.Name = "Position";
                    var cell6 = new DataGridViewTextBoxCell();
                    col6.CellTemplate = cell6;
                    datagridview1.Columns.Add(col6);
                    //Datagridview1 Column7
                    var col7 = new DataGridViewColumn();
                    col7.DataPropertyName = "VIP";
                    col7.Name = "VIP";
                    var cell7 = new DataGridViewTextBoxCell();
                    col7.CellTemplate = cell7;
                    datagridview1.Columns.Add(col7);
 
                    da.Fill(ds);
                    datagridview1.DataSource = ds.Tables[0];
 
                    System.Data.SqlClient.SqlDataAdapter recordAdp = new System.Data.SqlClient.SqlDataAdapter();
                    conn.Open();
                    recordAdp.SelectCommand = comm;
                    SqlDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        txtCustomerName.Text = (String)reader["CustomerName"];
                        txtCustomerAddress.Text = (String)reader["Address"];
                        txtPostCode.Text = (String)reader["postCode"];
                        txtCustomerTel.Text = (String)reader["MainTelephone"];
                        txtProduct.Text = (String)reader["Product"];
                    }
 
                    reader.Close();
                    comm.Dispose();
                    cnn.Close();
 
 
                }
            }

Open in new window

0
Comment
Question by:wilko100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 2000 total points
ID: 24359376
I noticed you datagridview1 doesn't have a DataMember

datagridview1.DataMember = "";

Here is an article that might help

http://www.akadia.com/services/dotnet_databinding.html

A little more explanation of what is going wrong would be good.
0
 

Author Closing Comment

by:wilko100
ID: 31582409
Excellent, just what i was looking for. Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question