GridView DataBinding to a DataTable and row deleting problems

A

Andy B

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"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="Word"
HeaderText="Word"></asp:BoundField>
<asp:BoundField DataField="Definition"
HeaderText="Definition"></asp:BoundField>
</Columns>
</asp:GridView>

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;
StockContract.Dictionary.Rows.Add(Row);
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
again!";
}
DefinitionList.DataSource = StockContract.Dictionary;
DefinitionList.DataBind();
WordTextBox.Text = String.Empty;
DefinitionTextBox.Text = String.Empty;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top