1

我已经搞砸了很长一段时间了,它变得越来越没乐趣了;我按照MSDN 指南从数据网格中删除一行。它适用于任何行但是我无法指定行......基本上我可以通过使用CurrentIndex参数删除随机行我尝试更具体的任何内容都会让我出现转换错误。

简而言之,“FindByID”(我的主键)给了我“对象到长”错误等。无法确定我想要删除的行。

    //int ThisRow = radGridView1.CurrentIndex.Value;

    // Locate row for deletion
    VSConnectorDataSet.TestTableRow oldTestTableRow;
    oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(
                      Int64.Parse(radGridView1.CurrentRow.Cells["ID"].Value));
    // Delete the row from the dataset
    oldTestTableRow.Delete();

    // Delete from database
    this.testTableTableAdapter1.Update(this.vSConnectorDataSet.TestTable);

    //DataRow rowDel=vSConnectorDataSet.TestTable.Rows[ThisRow];
    //rowDel.Delete();
    //testTableTableAdapter1.Update(vSConnectorDataSet);
4

2 回答 2

0

Int64.Parse只接受一个字符串。也许尝试:

long selRowVal = (long)radGridView1.CurrentRow.Cells["ID"].Value;
oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(selRowVal);

或者

Int64.Parse(radGridView1.CurrentRow.Cells["ID"].Value.ToString());

目前尚不清楚它是什么数据类型Cell.Value。也许明确的演员表可能会有所帮助。

于 2010-08-01T19:55:28.103 回答
0

找到了如何 Convert.ToInt64 我现在可以通过绑定的 dataset-tableadapter-database 方法来定位我删除的行。

private void DeleteToolStripButton_Click(object sender, EventArgs e)
{
    long ThisRow = Convert.ToInt64((radGridView1.CurrentRow.Cells["ID"].Value));
    DialogResult DelEntry = MessageBox.Show("Do you want to delete the entry titled '" + radGridView1.CurrentRow.Cells["SparesTitle"].Value + "'?", "Delete this entry?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
    switch (DelEntry)
    {
        case DialogResult.OK:
            // Locate row for deletion
            VSConnectorDataSet.TestTableRow oldTestTableRow;
            oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(ThisRow);
            // Delete the row from the dataset
            oldTestTableRow.Delete();
            // Delete from database
            this.testTableTableAdapter1.Update(this.vSConnectorDataSet.TestTable);
            break;
            //
            // To Do - Give Slected to another row; having just deleted our 'CurrentRow'
            //
        case DialogResult.Cancel:
            break;
    }
}
于 2010-08-01T20:22:20.477 回答