Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagrid contains 14 empty rows. NOw, i want save the multiple record from this datagrid in the sql server 2008.



plzz, ANyone Can Help me.
Posted
Comments
Joezer BH 5-Feb-13 3:26am    
What seems to be the problem?

1 solution

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.

C#
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());
            //loop to read the data from data table
            foreach (DataRow dr in dt.Rows)
            {
                MessageBox.Show(dr[0].ToString()); //First col value
                MessageBox.Show(dr[1].ToString()); //second col value
                MessageBox.Show(dr[2].ToString()); //third col value
            }
            
        }

        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
 
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