代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
DataSet ds;
SqlDataAdapter sda;
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=.; database=db_test;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from [db_test].[dbo].[sheet1]", conn);//获取表中所有数据
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds, "cs");
dataGridView1 .DataSource =ds.Tables [0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt=ds.Tables ["cs"];
sda.FillSchema(dt,SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox1.Text);
dr["耕地面积"] = textBox2.Text.Trim();
dr["水田"] = this.textBox3.Text.Trim();
dr["旱田"] = this.textBox4.Text.Trim();
dr["村民小组"] =this. textBox5.Text.Trim();
dr["户数"] = this.textBox6.Text.Trim();
dr["总人口"] =this. textBox7.Text.Trim();
dr["农民人均纯收入"] = this.textBox8.Text.Trim();
dr["农林牧渔业总产值(现价)"] = this.textBox9.Text.Trim();
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedCells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedCells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedCells[8].Value.ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
5 个解决方案
#1
你先看看SelectedCells有没有,你访问那么多,不存在的话肯定报错
#2
不好意思啊,才看到回复!
#3
dr["耕地面积"] = textBox2.Text.Trim();
dr["水田"] = this.textBox3.Text.Trim();
dr["旱田"] = this.textBox4.Text.Trim();
dr["村民小组"] =this. textBox5.Text.Trim();
dr["户数"] = this.textBox6.Text.Trim();
dr["总人口"] =this. textBox7.Text.Trim();
dr["农民人均纯收入"] = this.textBox8.Text.Trim();
dr["农林牧渔业总产值(现价)"] = this.textBox9.Text.Trim();
这里总共 8个单元格
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedCells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedCells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedCells[8].Value.ToString();
这里却是9个单元格。。。。。。所以肯定超出了最大索引
dr["水田"] = this.textBox3.Text.Trim();
dr["旱田"] = this.textBox4.Text.Trim();
dr["村民小组"] =this. textBox5.Text.Trim();
dr["户数"] = this.textBox6.Text.Trim();
dr["总人口"] =this. textBox7.Text.Trim();
dr["农民人均纯收入"] = this.textBox8.Text.Trim();
dr["农林牧渔业总产值(现价)"] = this.textBox9.Text.Trim();
这里总共 8个单元格
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedCells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedCells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedCells[8].Value.ToString();
这里却是9个单元格。。。。。。所以肯定超出了最大索引
#4
SelectedCells应该是选中单元格的集合吧,你放在CellClick事件里,执行时肯定只有一格被选中啊,第二句应该就报错了
#5
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1. SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = dataGridView1 .SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text = dataGridView1. SelectedRows[0].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
textBox2.Text = dataGridView1. SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = dataGridView1 .SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text = dataGridView1. SelectedRows[0].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
#1
你先看看SelectedCells有没有,你访问那么多,不存在的话肯定报错
#2
不好意思啊,才看到回复!
#3
dr["耕地面积"] = textBox2.Text.Trim();
dr["水田"] = this.textBox3.Text.Trim();
dr["旱田"] = this.textBox4.Text.Trim();
dr["村民小组"] =this. textBox5.Text.Trim();
dr["户数"] = this.textBox6.Text.Trim();
dr["总人口"] =this. textBox7.Text.Trim();
dr["农民人均纯收入"] = this.textBox8.Text.Trim();
dr["农林牧渔业总产值(现价)"] = this.textBox9.Text.Trim();
这里总共 8个单元格
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedCells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedCells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedCells[8].Value.ToString();
这里却是9个单元格。。。。。。所以肯定超出了最大索引
dr["水田"] = this.textBox3.Text.Trim();
dr["旱田"] = this.textBox4.Text.Trim();
dr["村民小组"] =this. textBox5.Text.Trim();
dr["户数"] = this.textBox6.Text.Trim();
dr["总人口"] =this. textBox7.Text.Trim();
dr["农民人均纯收入"] = this.textBox8.Text.Trim();
dr["农林牧渔业总产值(现价)"] = this.textBox9.Text.Trim();
这里总共 8个单元格
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedCells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedCells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedCells[8].Value.ToString();
这里却是9个单元格。。。。。。所以肯定超出了最大索引
#4
SelectedCells应该是选中单元格的集合吧,你放在CellClick事件里,执行时肯定只有一格被选中啊,第二句应该就报错了
#5
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1. SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = dataGridView1 .SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text = dataGridView1. SelectedRows[0].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
textBox2.Text = dataGridView1. SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = dataGridView1 .SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text = dataGridView1. SelectedRows[0].Cells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox9.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();