Hi,
I want to add rows to the datatable .. I may have to add one row or multiple rows.. No of rows to be added to the Datatable may vary.. How to do this?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
B M SuchitraPosted Dec 8, 2011, 5:17 AM
Actually i am adding a row to DATATABLE at linkbutton click.. This DATATABLE i am binding it to Gridview. After binding if user adds one more row to DATATABLE(TELL ME HOW TO ADD THAT ALSO)
Then the DATATBLE should display both the rows ie also the previously added row also..
in my case datatable is displaying only currently added row and not the previous row,.. How to show all the rows..
Point to note: i am not saving the row to database
Rajkumar RPosted Dec 6, 2011, 6:42 AM
B M SuchitraPosted Dec 6, 2011, 6:35 AM
What is that final in the for loop?
Rajkumar RPosted Dec 6, 2011, 4:03 AM
dtable.AcceptChanges();
Try this in your gridbind function
it may help u
Alok PandeyPosted Dec 5, 2011, 11:54 PM
I hope following code will help you..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt = new DataTable(); {
private void Form1_Load(object sender, EventArgs e)
{
DataColumn dcname = new DataColumn("NAME");
DataColumn dccity = new DataColumn("CITY");
dt.Columns.Add(dcname);
dt.Columns.Add(dccity);
dataGridView1.DataSource = dt;
}
private void btnAddToGridview_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr[0] = txtname.Text;
dr[1] = txtcity.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}
SqlConnection conn;
SqlCommand comm;
string constring = @"Data Source=server_name;Initial Catalog=DATABASE_NAME;Integrated Security=True";
private void btnSaveToDatabase_Click(object sender, EventArgs e)
{
conn = new SqlConnection(constring);
conn.Open();
for(int a=0;a
comm = new SqlCommand("insert into TABLE_NAME values ('" + dataGridView1.Rows[a].Cells[0].Value + "','" + dataGridView1.Rows[a]
.Cells[1].Value + "')", conn);
comm.ExecuteNonQuery();
}
conn.Close();
MessageBox.Show("Saved");
}
}
}
B M SuchitraPosted Dec 5, 2011, 10:34 PM
I have a gridview with empty data template which has a row after adding data to this row i need to add this row to datatable and bind this table to gridview. Again i want to add one more rows data to the datatable and display this row along with the previously added row so now i have to display 2 rows ...
My problem is i have added one row and it displays fine later when i add one more row i am losing the previous rows data.and only currently added row is displayed.. Please suggest how to overcome this problem
dev 0Posted Dec 5, 2011, 7:40 AM
If you want to add rows from database using table/stored procedure into the Datatable ,
then you can try this..
SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ToString();
SqlCommand cmd = new SqlCommand(SprocName, sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sqlcon.Open();
da1.Fill(dt);
da1.Dispose();
sqlcon.Close();
Hope this will help you.
Thanks.
Datta KharadPosted Dec 5, 2011, 6:42 AM
Where is stored your rows either datatable or any other (it means source of getting rows)...which you want to insert?
B M SuchitraPosted Dec 5, 2011, 3:36 AM
I may add one row or multiple. I do not know how many rows i will add.. Then how to count the rows and then add to the datatable?
Datta KharadPosted Dec 3, 2011, 2:00 AM
dtData = new DataTable();
dtData.Columns.Add("Name");
dtData.Columns.Add("Address");
dtData.Columns.Add("Mark");
DataRow dr = dtData.NewRow();
dr[0] = "Datta";
dr[1] = "Mumbai";
dr[2] ="500";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "DK";
dr[1] = "Pune";
dr[2] = "8000";
dtData.Rows.Add(dr);
dr = dtData.NewRow();
dr[0] = "Kharad";
dr[1] = "Hyderbad";
dr[2] = "45000";
dtData.Rows.Add(dr);
//OR....If you want to copy rows of datatable1 to datatable2
foreach (DataRow dr in datatable1.Rows)
{
object[] row = dr.ItemArray;
datatable2.Rows.Add(row);
}