GridView DataBinding to a DataTable and row deleting problems

Discussion in 'ASP .Net' started by Andy B, Apr 30, 2008.

  1. Andy B

    Andy B Guest

    I have the following gridview. There are 2 other textBoxes and a button on
    the page as well. When somebody puts text into the 2 textBoxes and presses
    the add button, it puts the values of those textBoxes into the DataTable.
    The gridView shows what values have already been entered into the DataTable.
    <asp:GridView ID="DefinitionList" runat="server" Caption="Dictionary
    entries" EmptyDataText="There are no definitions yet." AllowPaging="True"
    AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True" OnRowDeleting="DefinitionList_RowDeleting"
    <asp:BoundField DataField="Word"
    <asp:BoundField DataField="Definition"

    Here is the code that runs the add button. Everything works fine here the
    way I would want it to. The problem comes in with the gridView above. When I
    press the delete button, it wipes out the whole DataTable (all of the rows)
    not just one of them. How would I make it delete only the row I want to get
    rid of?
    private void AddDefinition() {
    ///Todo: Add validation for empty values
    Contract StockContract = (Contract)Session["StockContract"];
    //Create a linq query to determine if the word already exists in the
    dictionary. If it does, return the GridView caption as an error.
    var Query = from Counter in StockContract.Dictionary.AsEnumerable() where
    Counter.Field<string>("Word") == WordTextBox.Text
    select new { Word = Counter.Field<string>("Word") };
    if(WordTextBox.Text==String.Empty || DefinitionTextBox.Text ==
    String.Empty) {
    DefinitionList.Caption = "Empty entries aren't allowed! Type a word and
    definition in the boxes above and press the add button.";
    //Since the word didn't exist in the dictionary, go ahead and add it.
    else if(Query.Count()==0) {
    DataRow Row = StockContract.Dictionary.NewRow();
    Row["Word"] = WordTextBox.Text;
    Row["Definition"] = DefinitionTextBox.Text;
    DefinitionList.Caption = "Dictionary entries";
    //resave after every add of a word to the dictionary
    Session["StockContract"] = StockContract;
    } else {
    DefinitionList.Caption = "That word is already in the dictionary, try
    DefinitionList.DataSource = StockContract.Dictionary;
    WordTextBox.Text = String.Empty;
    DefinitionTextBox.Text = String.Empty;
    Andy B, Apr 30, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.