none
how to delete row from a typed dataset in c#?

    Question

  • hi

    I have a typed dataset, 2 textboxes and a button named Add. When user enter Account Number and Amount into textboxes and click Add button am saving it in a typed dataset  and also adding it to datagridview like this.

    FtsDataSet is my DataSet object and Account_Table is my table name.

    private void AddToDataGridView()
    {
    var dtAccount = this.FtsDataSet.Account_Table.FirstOrDefault(xx => xx.ACCT_NO == txtAccountNumber.Text);
    dtAccount.Amount = Convert.ToDecimal(this.txtAmount.Text);
    var dtGridTable = from xx in this.FtsDataSet.Account_Table.AsEnumerable()
    where !xx.IsAmountNull() 
    select new
    {
    SEQ = i, ACCT_NO = xx.ACCT_NO,
    Amount = xx.IsAmountNull() ? "" : xx.Amount.ToString(),
    };
    this.dgrdCreditAmtDetails.Visible = true;
    this.dgrdCreditAmtDetails.AutoGenerateColumns = false;
    this.dgrdCreditAmtDetails.DataSource = dtGridTable.ToList();
    }
    
    btnAdd_Click()
    {
     i++;
     AddToDataGridView();
    }

    When i click Add button i am saving data to DataSet and also displaying data in datagridview. My requirement is  to display record sequence number but it is displaying Seq No as 1 for first record and 2 and 2 for first and second record. e.g like this

    SEQ       AccountNumber      Amount

    1             abc                       10                   when i add another record it display as

    SEQ       AccountNumber       Amount

    2            abc                       10

    2           xyz                        30                How to set sequence to SEQ row ? and also I have a delete button in datagridview and when i delete Button i have to remove row from datagridview and also from dataset FtsDataSet ? How ? Please help?

    Wednesday, June 09, 2010 1:57 PM

Answers

All replies

  • For the sequence it appears you are incrementing i in the add click and then using this value as the sequence for all rows in your select. Try removing i++ in the add click and update your linq query to SEQ=i++.  This will call the increment for each row when the query is executed.

    As for deleting from the grid and the dataset. When you setup your strong typed dataset, it should have generated table adapters. In your case it should be something along the lines of FtsDataSetTableAdapters.Account_TableAdapter. These adapters are what you should use to manage the retrieving and persisting of data changes. The adapters have queries on them and if you just go with the defaults it should have a Delete() that takes the primary key as a value. Once you call delete on the adapter, then just rebind and your changes will be reflected in the grid.

    One thing that may help as well. I obviously don't know the full scope of your application but from what I see it appears you are taking the account_table as a source, selecting from it with filters to create the dtgridtable. You could take care of some of this in your tableadapter Fill and GetData queries or even just use the datasource expressions. The adapter queries have properties that allow you to specify how to treat nulls and you could just select the columns you require in your datatable.


    Chris Wigley MCAD\MCPD
    Thursday, June 10, 2010 5:04 AM
  • hi

    When i tried to remove i++ from button click and added in the linq query as SEQ = i++, am getting sequence number in datagridview as

    SEQ       AccountNumber      Amount

    1             abc                       10                   when i add another record it display as

    SEQ       AccountNumber       Amount

    2            abc                       10

    3           xyz                        30  

    SEQ       AccountNumber       Amount

    3            abc                       10                  

    4           xyz                        30                  when i add another record it display as

    5            ttt                          40

    how to solve this?

    Saturday, June 12, 2010 5:46 AM
  • Hi AristocratP,

    For questions of how to delete row from DataSet when it removed from DataGridView, you can refer to:

                      http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.adonet/topic35501.aspx
                      http://msdn.microsoft.com/en-us/library/feh3ed13(VS.80).aspx
                      http://www.codeguru.com/forum/showthread.php?t=256573

    Hope them help.

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, June 15, 2010 8:51 AM
    Tuesday, June 15, 2010 7:08 AM
  • hi

    I got it using the below code

    if (MYDataSet.MyTable.Select("ACCT_NO='" + sAcctNumber + "'").Length > 0)
          {
            DataRow drAcctNo = MYDataSet.MyTable.Select("ACCT_NO='" + sAcctNumber + "'")[0];        
            //this.MYDataSet.MyTable.Rows.Remove(drAcctNo);
            drAcctNo.Delete();
            
          }
    Thanks,
    • Marked as answer by Helen Zhou Tuesday, June 15, 2010 8:51 AM
    Tuesday, June 15, 2010 7:30 AM