int result;
if (dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("是否删除","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows].Cells[0].Value.Tostring() == "")
{
MessageBox.Show("请选择一行删除");
string[] array=new string[0];
for(int i=0;i<this.dataGridView1.SelectedRows.Count;i++)
{
string Res=this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
string Result="delete from Bookcollection where BookName='"+Res+"'";
array [i]=Result;
}
foreach(string str in array)
{
this.Update(str);
}
}
}
}
就解!错误 1 与“System.Windows.Forms.DataGridViewSelectedRowCollection.this[int]”最匹配的重载方法具有一些无效参数 I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 92 25 WindowsApplication1
错误 2 参数“1”: 无法从“System.Windows.Forms.DataGridViewSelectedRowCollection”转换为“int” I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 92 57 WindowsApplication1错误 3 “Update”方法没有采用“1”个参数的重载 I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 104 29 WindowsApplication1
if (dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("是否删除","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows].Cells[0].Value.Tostring() == "")
{
MessageBox.Show("请选择一行删除");
string[] array=new string[0];
for(int i=0;i<this.dataGridView1.SelectedRows.Count;i++)
{
string Res=this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
string Result="delete from Bookcollection where BookName='"+Res+"'";
array [i]=Result;
}
foreach(string str in array)
{
this.Update(str);
}
}
}
}
就解!错误 1 与“System.Windows.Forms.DataGridViewSelectedRowCollection.this[int]”最匹配的重载方法具有一些无效参数 I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 92 25 WindowsApplication1
错误 2 参数“1”: 无法从“System.Windows.Forms.DataGridViewSelectedRowCollection”转换为“int” I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 92 57 WindowsApplication1错误 3 “Update”方法没有采用“1”个参数的重载 I:\lizi\WindowsApplication1\WindowsApplication1\Form_ReadBook.cs 104 29 WindowsApplication1
另外,在说个问题```
if (this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows].Cells[0].Value.Tostring() == "")
{
MessageBox.Show("请选择一行删除");
return;
}
ELSE
{
//代码实现
}
应该要这样吧.代码对不对,不知道.
帮你顶下```
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 JieDa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds;
SqlDataAdapter da;
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
ShowDGV();
} private void ShowDGV()
{
string sql = "select * from b";
ds = new DataSet();
da = new SqlDataAdapter(sql, DBHelper.StringCon);
da.Fill(ds, "b");
dataGridView1.DataSource = ds.Tables["b"];
} private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否删除","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information)== DialogResult.Yes)
{
this.dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
SqlCommandBuilder buider = new SqlCommandBuilder(da);
da.Update(ds, "b");
MessageBox.Show("删除成功!");
}
}
}
}
貌似DGV里面是默认选中的
一般情况下 你只要使用 dataGridView1.Rows.RemoveAt(nRowIndex);
nRowIndex 是你要删除行的ID,从0开始计数的。