Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
i want to connect to db and read data from a table then show it in grid view but after do this tabel header add to my gridview header but i want to data be in my gride view
my code is:
 DataTable k = new System.Data.DataTable();
            try
            {
 
                string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
                Qconnection.ConnectionString = str;
                Qcommand.Connection = Qconnection;
 

                string commandText = "select name,lname,phone,mobile,address from moshtari where name=@name and lname=@lname";
                Qcommand.Parameters.AddWithValue("@name", textBox3.Text);
                Qcommand.Parameters.AddWithValue("@lname", textBox4.Text);
                Qcommand.CommandText = commandText;
                Qcommand.CommandType = CommandType.Text;
                SqlCeDataAdapter a = new SqlCeDataAdapter();
                a.SelectCommand = Qcommand;
                a.Fill(k);
                Qconnection.Open();
                Qconnection.Close();
                dataGridView1.DataSource = k;
 
            }
            catch (Exception ex)
            {
 
                throw new Exception(ex.Message);
              
            }
Posted 6 Nov '12 - 22:25

Comments
saud_a_k - 7 Nov '12 - 4:27
dataGridView1.DataBind();
helloworld2020 - 7 Nov '12 - 4:30
And, what is error? Please, provide more information!
f.sarikhani - 7 Nov '12 - 4:31
c# not know this method
f.sarikhani - 7 Nov '12 - 4:36
i havenot error I want the gridview header that I set myself, but after running the tabel header add to my grideview
faisal23 - 7 Nov '12 - 5:16
go to gridview properties and set column name

4 solutions

Try this:
 
 
            try
            {
                 DataTable k = new System.Data.DataTable();
                string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
                Qconnection.ConnectionString = str;
                Qcommand.Connection = Qconnection;
 

                string commandText = "select name,lname,phone,mobile,address from moshtari where name=@name and lname=@lname";
                Qcommand.Parameters.AddWithValue("@name", textBox3.Text);
                Qcommand.Parameters.AddWithValue("@lname", textBox4.Text);
                Qcommand.CommandText = commandText;
                Qcommand.CommandType = CommandType.Text;
                SqlCeDataAdapter a = new SqlCeDataAdapter();
                
                 a.SelectCommand = Qcommand;
                
 
                  a.Fill(k);
                  BindingSource bSource = new BindingSource();
                                    
                  bSource.DataSource = k;
                          
                  dataGridView1.DataSource = bSource;
                
                Qconnection.Close();
                
 
            }
            catch (Exception ex)
            {
 
                throw new Exception(ex.Message);
 
            }
  Permalink  
Comments
f.sarikhani - 7 Nov '12 - 4:49
dont resolve my problem
If you're saying that you want to use your own names for the column headers istead of the field names defined in the database, you can do this:
 
...
Qconnection.Open();
Qconnection.Close();
dataGridView1.DataSource = k;
 
dataGridView1.Columns[0].Name = "My Name For Column 0";
dataGridView1.Columns[1].Name = "My Name For Column 1";
  Permalink  
You can change the HeaderText = "Column Name" in source code also.
  Permalink  
it seems you forgot below line after asigning datasource to DataGrid.
dataGridView1.DataBind();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 373
1 OriginalGriff 335
2 Arun Vasu 315
3 Maciej Los 218
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,680
1 OriginalGriff 7,539
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid