上面 一个图
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.Sql;
using System.Data.SqlClient;
namespace GridViewManagement
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connnectionstring = "server=.;database=Mag_Rol;Integrated Security=sspi";
DataTable table;
SqlDataAdapter adapter;
public void GetData(string selectcommand)
{
try
{
adapter = new SqlDataAdapter(selectcommand, connnectionstring);
table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
this.bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
} private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = bindingSource1;
GetData("select * from [USER]");
} private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
} private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DialogResult result = MessageBox.Show("确定要删除记录吗?", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
table.Rows[e.RowIndex].Delete();
}
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(table);
} //主要是理解这个地方
当我屏蔽 /*SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(table);*/
这个位置的时候 删除第一条位置成功 当删除 第二条记录的时候e.RowIndex=0,很容易理解
但是数据集中的table 的rowindex=1(按我的理解他应该更新为0)
似乎 他不能自己更新 只有 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(table);
来达到更新的目的
望各位解答 }
}
adapter.Update(table);
我有一个特别的需求
我需要在页面上删除 记录
但是 事实上 数据库里面的数据压根就不删除
不知道怎么样 处理