【问题标题】:Specifying the records to be deleted from a database using the TableAdapter.Update method使用 TableAdapter.Update 方法指定要从数据库中删除的记录
【发布时间】:2011-03-23 21:50:19
【问题描述】:

我已经把这个搞砸了很长一段时间了,它变得越来越没乐趣了;我按照MSDN guide 从数据网格中删除一行。它适用于 any 行但是我无法指定行...基本上我可以使用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);

【问题讨论】:

  • radGridView1.CurrentRow.Cells["ID"].Value 是什么?
  • @p.campbell FindByID 是 VS2008 对我的要求,是我在数据库中的主键(一个 INT),我无法将其值解析为 long(FindByID 的要求)。
  • @SLaks Telerik radGridView1.CurrentRow.Cells["ID"]。值会根据所选行而改变。
  • 它的实际价值是多少? (在调试器中)
  • @SLaks 为例,'11' 是值

标签: c# winforms data-binding .net-3.5 system.data.datatable


【解决方案1】:

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 是什么数据类型。也许明确的演员表可能会有所帮助。

【讨论】:

  • @confussedinwales:太好了,很高兴能帮上忙!在 StackOverflow 上,您可以为那些帮助您找到解决方案的人投票并“标记为答案”。在这种情况下,看起来转换/转换为 long 是正确的。如果您愿意,我很乐意投赞成票和绿色复选标记!
  • @p.cambell 在阅读您的回复后,我确实尝试过投票,但它说它需要 15 个代表!?!所以现在我意识到你可以有多个答案..... :)
【解决方案2】:

找到了如何 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;
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 2012-05-16
    • 2011-07-12
    • 2016-12-10
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多