|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A problem with the datagridI confront a problem with the datagrid. I create an .asp file. There are two buttons, one datagrid on the screen. The first button is "Show" button. When I click this button, it will call the data in the sql Server and bind the data to the datagrid. The code is: ArrayList alErrorIn = new ArrayList(); DataTable dtBookAdmin = objBOBookAminManipulation.GetBookAdminsInterface(out alErrorIn); dgBookAdmin.DataSource = dtBookAdmin; dgBookAdmin.DataBind(); It works fine. The second button is "Delete" button. There is a template colum in the datagrid. When I choose the item and click the "Delete" button, the corresponding items in the datagrid will be deleted. But it won't work fine. When I debug, I check the datagrid before I do the delete: private void btnDelete_Click(object sender, System.EventArgs e) { int iCount = dgBookAdmin.Items.Count; ArrayList alDeleteid = new ArrayList(); foreach(DataGridItem dataGridItem in dgBookAdmin.Items) .... But the iCount show that there is no item in the datagrid. But there are items showed in the screen and I check the value after I have bind the data to the datagrid, it's fine. So I get confused. Could anyone clear my problem? Thank you very much! Did you count the number of items in dbBookAdmin.DataSource ?
What you want might be something like ((DataTable)dbBookAdmin.DataSource).Clear() in order to remove all the items of the DataTable. Do you need to check the datagrid?
In your template column you have the code for the asp.net button control, why don't you add the row's unique id to the CommandArgument property of the delete button. Then use the Datagrids ItemCommand method which is fired whenever an item in the datagrid triggers a command, which the delete button will do. The code for the Item command is as follows: private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e) { if (e.CommandName == "Delete") { DeleteThisRecord(int.Parse(e.CommandArgument)); } } The code for the Button within the template control is as follows: <asp:button Runat=server ID="btnDelete" CommandArgument='<%# Databinder.Eval(Container.DataItem, "ID") %>' CommandName="Delete"></asp:button> I may have the syntax for the databinding statement wrong but you can look that up. By doing it this way it allows you to pass through the ID of the row you wish to delete, thus you don't need to check any records. You simply send the delete command to the database, and then re-databind your datagrid. Hope this helps Pete Thank you for your suggestion. But, it is not working.
When I came to the Itemcommand function, any event I do on the datagrid is not work. So I check if the data is still there in the datagrid, and find that all the data I binded successful to the datagrid is lost. That's why I check the numbers of the rows in the datagrid. And the number is 0. And I don't know why is that. |
|||||||||||||||||||||||