By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

GridView DataBinding to a DataTable and row deleting problems

P: n/a
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;
}
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.