Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datatable within a dataset , i'm not using tableadapter i just want to add rows into the table
Posted
Updated 27-Oct-11 0:57am
v2

try
            {
                DataTable dt = new DataTable();

                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Name", typeof(string));

                DataRow dr = dt.NewRow();
                dr["ID"] = 100;
                dr["Name"] = "Bala";
                dt.Rows.Add(dr);


               // dataGridView1.DataSource = dt;    

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);  
            }



UPDATE 1

1. Hope your dataset is declared as follows

C#
public partial class Form1 : Form
{
    DataSet ds = new DataSet();

    public Form1()
    {
        InitializeComponent();
    }


2. Hope you data binding is as follows

try
           {

               SqlConnection oconn = new SqlConnection();
               oconn.ConnectionString = "Data Source=Localhost;Initial Catalog=TEST_DB;user=sa;password=123";
               oconn.Open();

               SqlDataAdapter oda = new SqlDataAdapter("Select ID,Name from employees", oconn);
               oda.Fill(ds);
               dataGridView1.DataSource = ds.Tables[0];

           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message);
           }



3. You can add a new row to the table which is attached to a dataset as follows

C#
private void btnAdd_Click(object sender, EventArgs e)
       {
           try
           {
               DataRow dr = ds.Tables[0].NewRow();
               dr["name"] = "bala";
               ds.Tables[0].Rows.Add(dr);

           }
           catch (Exception ex)
           {


           }
       }
 
Share this answer
 
v2
Comments
snake1 27-Oct-11 7:20am    
hello Bala Selvanayagam , Thanks for your reply, it seems in your code that you are creating a new instance of a datatable how could i do that for the table which i have created into dataset ? ,because i have databinding to the members of this table
Bala Selvanayagam 27-Oct-11 7:59am    
Its all most similar and i have updated my above solution under the heading UPDATE 1 and hope this helps.


If this does not help, post your code showing how you are data binding and what do you mean by without dataadapter populating a dataset ?
P.Salini 27-Oct-11 8:00am    
My 5!
Bala Selvanayagam 27-Oct-11 8:01am    
Thanks CodeHelper
Try for this

C#
DataRow row;
//loop for datatables row
for (int index = 0; index < dt.Rows.Count; ++index)
{
row = dt.Rows[index];
// if u want to add new row u can add here
}
 
Share this answer
 
v3

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