Hi,
Please go through the below sample code. I have created a datatable and bind with datagrid, Now if I add a row in the grid, it will get added into the datatabel. When user clicks save button, read the data from the data table and insert into the database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dt.Rows.Count.ToString());
foreach (DataRow dr in dt.Rows)
{
MessageBox.Show(dr[0].ToString());
MessageBox.Show(dr[1].ToString());
MessageBox.Show(dr[2].ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("Name");
dt.Columns.Add("Number");
dt.Columns.Add("Age");
dataGridView1.DataSource = dt;
}
}
}
Best Regards
Muthuraja