Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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
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

Try this:

C#
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);

}
 
Share this answer
 
v2
Comments
f.sarikhani 7-Nov-12 4:49am    
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:

C#
...
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";
 
Share this answer
 
v2
You can change the HeaderText = "Column Name" in source code also.
 
Share this answer
 
it seems you forgot below line after asigning datasource to DataGrid.
C#
dataGridView1.DataBind();
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900