Mark
Mark

Reputation: 981

Editable GridView With Autogenerated Columns

I'm trying to make a very simple asp.net page that binds a GridView using Linq2SQL that has editable rows. I basically want to add 2 ButtonFields, "Edit" and "Delete". When Delete is clicked the row is deleted, when Edit is clicked each autogenerated column turns into a Textbox and is editable and a new button "Save" appears. When the "Save" button is clicked the page is reloaded and the edited entries updated in the DB. I'm not really worried about error checking or anything fancy, I just want a way to quickly edit tables.

I'm trying to make this generic so that I can edit any table using a DropDownList:

grd.DataSource = db.GetTable(ddlTable.SelectedItem.Text);
grd.DataBind();

<asp:GridView id="grd">
<Columns>
<asp:ButtonField id="edit" Text="Edit"/>
<asp:ButtonField id="delete" Text="Delete"/>
</Columns>
</GridView>

I guess I have two main questions:

1) Is it possible to do this with AutoGenerateColumns="true" so that every table is editable without having to hard code for each table?

2) Is there a straight forward way to do this with the built in GridView events without writing a custom control? I'm not opposed to eventually using a custom control, but I'd like to write that myself if necessary; I'm sort of trying to avoid that at this point.

Upvotes: 2

Views: 2808

Answers (1)

Malk
Malk

Reputation: 11993

1) GridView has "AutoGenerateDeleteButton" and "AutoGenerateEditButton" properties that you can set to true.

2) Bind the grid using a DataSource (probably LinqDataSource). In this way you can specify what happens in the edit and delete events directly from the mockup. It will also give you built in paging/sorting.

Upvotes: 2

Related Questions