Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:25pm
Comments
saud_a_k at 7-Nov-12 4:27am
   
dataGridView1.DataBind();
helloworld2020 at 7-Nov-12 4:30am
   
And, what is error? Please, provide more information!
f.sarikhani at 7-Nov-12 4:31am
   
c# not know this method
f.sarikhani at 7-Nov-12 4:36am
   
i havenot error I want the gridview header that I set myself, but after running the tabel header add to my grideview
faisal23 at 7-Nov-12 5:16am
   
go to gridview properties and set column name
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
f.sarikhani at 7-Nov-12 4:49am
   
dont resolve my problem
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can change the HeaderText = "Column Name" in source code also.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
0 OriginalGriff 392
1 CPallini 365
2 Sergey Alexandrovich Kryukov 348
3 BillWoodruff 264
4 George Jonsson 255
0 OriginalGriff 5,095
1 CPallini 4,285
2 Sergey Alexandrovich Kryukov 3,767
3 George Jonsson 2,941
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100