本帖最后由 黑马田杰 于 2013-2-2 16:37 编辑
源代码如下:
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 importRow
{
public partial class Form1 : Form
{
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter sda;
DataSet ds;
int intIndex;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//初始化
this.button1.Text = "加载数据";
this.button2.Text = "修改数据";
}
private void button1_Click(object sender, EventArgs e)
{
//加载数据
conn = new SqlConnection("server=SHADOW-PC\\SQLEXPRESS;database=test;uid=sa;pwd=123;");
sda = new SqlDataAdapter("select * from dbo.table_2", conn);
ds = new DataSet();
sda.Fill(ds, "cs");
this.dataGridView1.DataSource = ds.Tables["cs"];
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
this.dataGridView1.Columns.Width = 66;
}
this.dataGridView1.Columns[0].ReadOnly = true;
}
private void button2_Click(object sender, EventArgs e)
{
//修改数据
DataTable dt = ds.Tables["cs"];
DataTable dsShow = (DataTable)this.dataGridView1.DataSource;
dt.ImportRow(dsShow.Rows[intIndex]);
SqlCommandBuilder comBuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
intIndex = e.RowIndex;
}
}
}
直接在DataGridView控件中修改数据后点击button2按钮可修改成功,但发生如下运行时错误,请问这是什么原因?该如何解决?
|
|