Manually configure GridView to display and delete row
I will like to share a tip here on what I have discovered today. Things are getting real fun :)
When you drag a GridView from your toolbox to your designer and you plan not to rely on the Smart Link (is the arrow called Smart Link?) to select the data source, below are the steps you can consider.
Okay, I assume when you select your data source (including selecting your sql statement) you are allow to set enable delete and insert feature to your grid view. And that is not what you want.
Steps by Steps:
1. Create a stored procedure to return 3 columns.
SELECT NameID, Name, Description
FROM Table1
2. Right now, you have to add 3 databounds controls in your gridview. Use the smart link to add columns or edit columns.
*You don't have to add a datasource first before adding these columns
a) NameID BoundField
b) Name BoundField
c) Description BoundField
3. Now you have decided to have a delete button. You can do that as well in the edit columns or add columns.
d) Delete ButtonField
4. But again, you feel that you don't want to show NameID column because it is complicated to the users.
Try not to ignore step 5 and 6.
5. I think you need to remain the NameID column. All you can do is set that column visible = false in the Edit Columns. It won't display.
6. This is a very crucial step. If you don't have this you will have an error during runtime (compilation is okay).
Click on the GrdiView's properties and set DataKeyNames = NameID
7. Time to delete some records in gridview during runtime. You have to add an RowDeleting event.
protected void gvNames_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowIndex = e.RowIndex;
// The DataNameKey is crucial here. If not you will receive an error pointing to e.RowIndex
int nameID = (int)gvIndividualSearch.DataKeys[e.RowIndex].Value;
// Your business process here
}
8. Run your web application. I believe it will work on your side too :)
Cheers.