using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace demo38
{
public partial class Form1 : Form
{
SqlConnection conn;
DataSet ds;
SqlDataAdapter sda;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;database=test3;uid=sa;pwd=123456");
SqlCommand cmd = new SqlCommand("select * from Table_3", conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds, "Table_3");
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["Table_3"];
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();
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellContentClick(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();
}
}
}
錯誤

資料庫表格
uj5u.com熱心網友回復:
選定的只有一格,非要獲取5個選定單元格的資料,不過索引才怪了。uj5u.com熱心網友回復:
private void dataGridView1_CellContentClick(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();
textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
}
已經解決了,詳情去看這個人的介紹
https://blog.csdn.net/zhuzinanhua/article/details/82819777
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/82414.html
標籤:C#
