templatefield or button column - asp.net

I see a lot of code samples that use template fields for say a button column. However, asp has a ButtonField in gridview columns. What is the difference, if any, between quickly throwing a buttonfield column in a gridview over configuring a templatefield column?

Any pre-built column will be easier to use, and be automatically data bound. A templatefield can contain anything, buttons, 10 different buttons, another gridview, etc etc. So it can be whatever you want, but you need to deal with all the controls inside manually. If you are just looking for a column with one button, stick to the buttonfield control.

Related

New row in ASP.NET GridView Control

I want to do something really simple, I just can't seem to find the EnableClientAddRow property, so I can set it to true. I have a standard GridView control on a web form. I want a button to appear on the web form. When the user clicks the button, an empty row is added to the GridView UI, so the user can enter data in the appropriate fields. The row will of course, have a "Save" button of some type in one of the columns.
I know this functionality must be in the GridView somewhere, I just can't find it. I did find some odd hacks that try to manually implement this. I'm not really interested in footer manipulations or binding tricks, just the standard add row method.
EDIT:
It appears the GridView does not support adding a row as a first-order operation. This appears to be a serious design flaw.
I typically add a new record to the underlying data source as a part of the "add record" button click action. I then re-bind the view in order to show the blank row.
The new record is typically a DataRow if the GridView is bound to a DataTable, or an object if the GridView is bound to a collection of a particular type. Not sure if that is what you consider a binding trick from your question, but it works well and is quite easy to implement.
Edit - more detail to describe the process:
Add the row to the data source, set the EditItemIndex to the newly added row in order for the row to enter edit mode, then bind the data source to the GridView. Your EditItemTemplate would contain a Cancel and a Save button. Cancel would re-bind the GridView to the underlying data source without the empty row and set EditItemIndex to -1, thereby removing the row from the GridView.
How to easily insert row in GridView with SqlDataSource
If you add a new row to the datasource, even if the row has empty values, and you databind the datasource to the Gridview, it should show up as an editable row just like any of the other rows.

GridView Making a single Edit Item Template take the entire row

I've got this gridview. The gridview uses TemplateFields as the number of fields in the database's table. What I do next is use an ItemTemplate to present the correct column info.
Now, problems rise when the user click's Edit. Since I can only use EditItemTemplate to edit I am resulted with a control in each column. What I really want is to have a single row with no columns so I can easily style the edit mode (having a table with a different layout for example).
Is this possible? If so, how?
I suspect you will have to create your own specialized class that inherits from GridView and does its own rendering to accomplish this.
You might hack some workaround though... such as manually editing the rendered HTML output or DOM.

ASP.NET GridView - Editing Dynamic Template Columns

I have created a GridView whose columns are dynamically created based on my data source. I have implemented these columns by using the approach described here.Those columns display properly on the initial load. However, I need to implement commanding so that a user can edit / delete a row in the GridView.
At this point, I have implemented commanding as I would with a normal GridView. I have a TemplateField with an ItemTemplate that has LinkButton elements for edit and delete. The CommandName for each LinkButton is set to either Edit or Delete respectively.
Oddly, when a user clicks either the Edit or Delete link, the data in the GridView disappears. However, I have verified that I am in fact re-binding the data when one of these LinkButton elements is selected.
Can anyone provide some suggestions as to what the cause could be?
Thank you!
Here are good examples. You can figure out postback issue.
http://quickstarts.asp.net/QuickStartV20/aspnet/

Insert BoundField into DetailsView

Basically I have a detailsview, dependent on what query it is I have certain BoundFields I'd like to show or hide.
Using visual basic, I know that I can use .visible = false. That does the trick, however, it sacrifices my alternating row style in the process.
Any ideas?
Thanks
By the way if you write it in C# I will probably understand.
What you need to do is create several asp:panel objects. Inside each panel object, put a single DetailsView. In your codebehind, you will want to show the panel that contains the proper DetailsView for your query, and hide the rest of the panels.

How to add different controls in Gridview?

I know it's possible to change some columns in GridView controls to check boxes and when you are editing certain rows, the cells being hi-lited become text boxes, but supposed I want to add other controls in my gridView. For example: in the image below how would I change the entries of CategoryName to be a dropDown box of possible choices?
[Full size]
Use the TemplateField and define what controls are in your EditItemTemplate.
Read this article as a starting point.

Resources