Click here to Skip to main content
16,004,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to c#.

I have return the following code to add data to the Datatable.
C#
private void addbtn_Click(object sender, EventArgs e)
       {
          SqlCommand cmd;
           SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");

           con.Open();
           cmd=new SqlCommand("Insert into grossarynames values(' "+glisttxt.Text+" ')",con);
           cmd.ExecuteNonQuery();

       }

It is adding the values to the database.

But the data is not displayed in the DataGrid view please tell the code to display the data to the database.
Posted
Updated 22-Dec-11 19:36pm
v2
Comments
Sandeep Mewara 23-Dec-11 1:37am    
What datagridview? In current code, there is no mention of any datagrid! Please update the question and add more details.
Scubapro 23-Dec-11 1:44am    
Off course it's not displayed. If you have a data-bounded datagridview, you'll have to perform a requery (database) and refresh your datagridview.
kamath2012 23-Dec-11 2:03am    
in the windows form if i get the grid view task and click priview data the table entries are shown.But it is not displayed in the gridview? how to code that?
kamath2012 23-Dec-11 1:57am    
how to get the databounded datagridview?
kamath2012 23-Dec-11 2:01am    
in the windows form if i get the grid view task and click priview data the table entries are shown.But it is not displayed in the gridview? how to code that?

C#
private void addbtn_Click(object sender, EventArgs e)
        {
            SqlCommand cmd,read1;
            SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");
            con.Open();
            cmd=new SqlCommand("Insert into grossarynames values(' "+glisttxt.Text+" ')",con);
            cmd.ExecuteNonQuery();
            read1=new SqlCommand("Select * from grossarynames,con);
            SqlDataReader dr=read1.executeReader();
            DataTable dt=new Datatable();
            dt.Load(dr);
            datagridview.source=dt;
        }


check code
 
Share this answer
 
Try
C#
SqlCommand myCommand = new SqlCommand("your SELECT command");
SqlDataAdapter oAdapter = new SqlDataAdapter(myCommand); // You have a sqlDataAdapter which executes your command
dataset ds = new dataset();
oAdapter.Fill(ds);
myGridview.datasource = ds;
myGriview.databind();  // and then finally bind it to your GridView in your UI layer like this

More Reference Link :- http://www.dotnetperls.com/datagridview-tutorial[^]
 
Share this answer
 
Comments
[no name] 27-Dec-11 8:15am    
myGriview.databind();
it is used in web applications
use below code to display the data:

C#
SqlCommand cmd;
            SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");
 
            con.Open();
            cmd=new SqlCommand("Select * from grossarynames",con);
SqlDataAdapter da = new SqlDataAdapter(cmd); 
            cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds
DataGrid1.DataMember = "grossarynames";
 
Share this answer
 
v2
Try it
C#
if (ca.con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ca.da = new SqlDataAdapter("SELECT * from newproduct ", con);
            DataSet ds = new DataSet();
            ca.da.Fill(ds,"newproduct");
            dataGridView1 .DataSource =ds.Tables ["newproduct"];
            con.Close();
}
 
Share this answer
 
v2

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