I'd like to tinker with the auto-generated columns in a gridview a bit. What event would I want to override to modify them just after they are generated but before the control is rendered?
Check out this link for more information on Customizing the Auto-Generated Columns of a GridView Control:
http://msdn.microsoft.com/en-us/library/cc903950(VS.95).aspx
The particular event you are looking for would be the AutoGeneratingColumn event
More information on the AutoGeneratingColumn event
The columns are added to the GridView when the DataBind() method is called
Those two links are for the Datagrid not Gridview.
Related
I have a GridView which is completely user defined. The first column of the GridView is from the ListBox1. The headers are defined using ListBox2.
There fore I need to update rows of the GridView with some integer value and make sure the Gridview holds that value until a button is clicked.
Once the button is clicked I want to read each columns of the GridView that has values entered and create a table in the database with the headers names as columns and just replicate the GridView as table in database.
I have attached a screenshot of my GridView. Kindly help me fix this problem.
There are 2 key things to implement UPDATE logic in a GridView
In the GridView markup, you have to add an eventhandler for OnRowUpdating event
<asp:GridView ID="XXX" runat="Server" .....
OnRowUpdating="UpdateRecord" ..........>
Then you need to implement this "UpdateRecord" method referred above with your custom code which will basically retrieve the data from the grid row and update it to your datasource
You can refer to some sample code of how this can be achieved here
To point out again, in your case, since you want to dynamically create the table as well, you will need to additionally do that as well. Personally, i have no clue why you would go with such a design but in absence of any information, i assume you know best.
I have doubt about the Row_DataBound and Row_Created events :
What is the difference between Row_DataBound and Row_Created events?
What are the parameters for choosing between these two events?
RowCreated occurs after a row and all of its child controls are created.
RowDataBound occurs after the row (and its controls) are databound, i.e. populated with the data values.
The answer about which to use really depends on if you need the databound values or not. e.g. If you wanted to change the background color of your row based the value of one of your fields, then you would have to use the RowDataBound event. If your logic doesn't depend on the data, then I don't think it matters which event you use.
An example of where you would have to use RowCreated is if you had a drop down list in your row that would need to be popluated with values before the selected value was databound.
Hi I was just wondering what is the best way to fetch gridview data using row databound event of gridview. I am previously used to Eval but read its not recommended as it uses reflection.
What do you mean "Fetch" the data, by the time the the RowDataBound event has triggered, there already has to be a row of data, that is why the event has executed. If you want to access and mapipulate the data then it is in e.Row.DataItem.
Edit
To answer your comment, using Eval in the markup and putting code in the RowDataBound event handler and accessing e.Row.DataItem tend to be used under different cirumstances. If all you want to do is take the data and bind it to the property of a control then using Eval() (or Bind() for that matter) is fine. However if you want to do something more complex then you might need to do it in the RowDataBound event handler. For exam-ple you might have a grid of customer accounts and ballances. For customers where thir ballances are overdue you might want to turn the row red to highlight the fact that thier accounts are overdue. You could not do that using Eval or Bind in the markup so you would check e.Row.DataItem in the RowDataBound event handler and then decide whether to change the colour of the row.
How can I get the row leave event of a GridView?
The GridView does not have a row leave event.
See the list of existing events here.
i want to know how to edit a single row (which i select) from a data grid
for example i have a datagrid with columns A, B and C
and i have a couple rows of data, approx 10 rows.
lets say i want to change the value of data within row 4.
how would i do this?
i am using visual studio 2003, but i guess if visual studio 2005 would be okay too. for the coding I'm using c#
thanks..
All grid-like components of asp.net have the same machanism as it comes to starting to edit a single row. Actually it's default for asp.net only to edit a single row in a grid.
Needed to start editing is to include asp:button or asp:linkbutton in the ItemTemplate with the CommandName set to "Edit". This one of reserved commandnames all grid-like components knows how to respond to. Clicking this button in DataGrid will raise the EditCommand Event. In this event you have to set the EditItemIndex of the grid equal to Item.Itemindex of the eventargs. This will render the row vaccordeing to the EditItemTemplate.
In this template you put 2 buttons or linkbuttons. One should have the CommandName set to "Update" and one should have the CommandName set to "Cancel".
The "Update" button raises the UpdateCommand event. In which you execute code that store the data in the row to its storage (eg.: database) and sets the EditItemIndex to -1 --> all rows are rendered read-only(ItemTemplate or AlternateItemTemplate).
The "Cancel" button raises the CancelCommand event. In the event handler you have to do si set the EditItemIndex to -1.
This description is only true for DataGrid en not for the in asp.net introduced GridView which handles most of this "Boilerplate"code it self working together with the datasource controls. Google the web for more info on this. it's to much to explain here right now.
Hope it helps?
Take a look at the documentation for adding an EditItemTemplate to your datagrid. You use the ItemTemplate for view-only, display elements and you use the EditItemTemplate for controls used to bind against a single row that you select.
Here's a link that might help:
http://www.gridviewguy.com/
Is your data in a DataTable before making it a DataGrid, or can you put it in a DataTable? You can update/delete/edit rows in a DataTable, here's a link with code snippets, pretty straight forward:
http://msdn.microsoft.com/en-us/library/tat996zc(VS.80).aspx