如何根据在wpf文本框中输入的值从数据集中删除行? [英] How can I delete a row from a dataset based on a value entered in a textbox in wpf?

查看:79
本文介绍了如何根据在wpf文本框中输入的值从数据集中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btndelete_Click(object sender, RoutedEventArgs e)
        {
            int id=Convert.ToInt32(deptno_txt.Text);
            foreach (DataRow dr in ds.Tables[0].Rows)
           {
               if (id == 101)
               {
                    ds.Tables[0].Rows[0].Delete();
                }
              else if (id == 102)
               {
                    ds.Tables[0].Rows[1].Delete();
               }
                else
                    ds.Tables[0].Rows[2].Delete();
            }



我正在尝试这种方式.它正在工作,但是如果我有1000条记录,则变得很困难.因此,我需要简单的代码来删除数据集中的记录,以便可以使用commandbuilder更新到数据库.

 私有  void  btndelete_Click(对象发​​件人,RoutedEventArgs e)
        {
             for ( int  nRow =  0 ; nRow< ; = ds.Tables [ 0 ].Rows.Count; nRow ++)
            {
               如果(ds.Tables [ 0 ].行[ 0 ] [" ].ToString().Equals(txtbox.Text))
                {
                  ds.Tables [ 0 ].Rows [nRow] .Delete();
                  ds.Tables [ 0 ].AcceptChanges();
                  --nRow; // 如果ID在数据集中多次出现.
                   break ; // 否则请使用break从循环中出来.
                }
           
            }
        } 


这里的ID->是您的列名
< =->仅使用不小于等于

干杯:)


这不起作用.请使其正确.我只能删除第一行


private void btndelete_Click(object sender, RoutedEventArgs e)
        {
            int id=Convert.ToInt32(deptno_txt.Text);
            foreach (DataRow dr in ds.Tables[0].Rows)
           {
               if (id == 101)
               {
                    ds.Tables[0].Rows[0].Delete();
                }
              else if (id == 102)
               {
                    ds.Tables[0].Rows[1].Delete();
               }
                else
                    ds.Tables[0].Rows[2].Delete();
            }



I''m trying in this fashion. It''s working but if I have 1000 records then it becomes difficult. So I want simple code that deletes records in dataset so that I can update to database using commandbuilder.

解决方案

Hi vikky423,

private void btndelete_Click(object sender, RoutedEventArgs e)
        {
            for(int nRow=0;nRow<=ds.Tables[0].Rows.Count;nRow++)
            {
               if(ds.Tables[0].Rows[0]["Id"].ToString().Equals(txtbox.Text))
                {
                  ds.Tables[0].Rows[nRow].Delete();
                  ds.Tables[0].AcceptChanges();
                  --nRow;  // If Id comes multiple times in Dataset.
                  break;  // else use break it come out from loop.
                } 
           
            } 
        }


Here Id --> is your Column Name
<= --> use only less than not less than equal

Cheers :)


this is not working.please make it proper.i''m able to delete only 1st row


这篇关于如何根据在wpf文本框中输入的值从数据集中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆