using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace gridview
{
public partial class Form1 : Form
{
int m = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
cn.Open();
SqlCommand cm = new SqlCommand("select * from data_customer",cn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cm;
DataSet ds = new DataSet();
da.Fill(ds);
m = ds.Tables[0].Rows.Count;
// dataGridView1.DataSource = ds.Tables[0];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1[0, i].Value = ds.Tables[0].Rows[i][0].ToString();
dataGridView1[1, i].Value = ds.Tables[0].Rows[i][1].ToString();
dataGridView1[2, i].Value = ds.Tables[0].Rows[i][2].ToString();
}
cn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
cn.Open();
for (int i = m; i < dataGridView1.Rows.Count-1; i++)
{
SqlCommand cm = new SqlCommand("insert into data_customer(cust_id,cust_name,cust_address) values(" + dataGridView1[0, i].Value + ",'" + dataGridView1[1, i].Value + "','" + dataGridView1[2, i].Value + "')", cn);
cm.ExecuteNonQuery();
}
MessageBox.Show("data inserted");
cn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||