Click here to Skip to main content
12,952,398 members (53,203 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 7-Nov-12 4:27am
   
dataGridView1.DataBind();
helloworld2020 7-Nov-12 4:30am
   
And, what is error? Please, provide more information!
f.sarikhani 7-Nov-12 4:31am
   
c# not know this method
f.sarikhani 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 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 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2017
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