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.
Related
I want to build a DataGridView, which if I choose one of the item of a DataGridViewComboBoxCell, then the other cells in the same row which should be TextBoxes in other rows will turn into ComboBoxes,does anyone knows how to do that?
It is like:
TextBox1|TextBox2|ComboBox1.Item1|TextBox3 |TextBox4 |TextBox5 |TextBox6
TextBox1|TextBox2|ComboBox1.Item3|TextBox3 |TextBox4 |TextBox5 |TextBox6
TextBox1|TextBox2|ComboBox1.Item2|ComboBox3|ComboBox4|ComboBox5|ComboBox6
If you want to do something when the Value in a DataGridViewCell changes then you should handle the CellValueChanged event of the grid.
If you want to place a cell of a specific type in a specific location in a DataGridView then you can do so using the grid's indexer, e.g.
myDataGridView[columnIndex, rowIndex] = new DataGridViewComboBoxCell();
In summary, handle the CellValueChanged event, use an if statement to test whether the Value is one that corresponds to a text box or combo box and, if the types of the other cells are not what they should be, replace them.
I am using UltraWebGrid from Infragistics for grid functionality in my ASP.NET 4.0 Application. I have enabled inplace editing for few columns in the grid. I wrote the logic for this event as
protected void UltraWebGrid_RowUpdating(object sender, RowUpdatingEventArgs e)
in code behind. Along with this I want to know which cell in the row triggered this event, meaning Updating which field in the row triggered this event. I found we have a Row property for RowUpdatingEventArgs Class but that gets the entire row. I want to know what cell, its column name etc.. in the row was dirty that made this event happen.
Any useful pointers on this one?
You would need to track this on your own and can see when a cell is updated by handling the client side AfterCellUpdate client side event. In this event you will know what cell triggered the change and will need to store that information in a hidden field or possible a hidden column of the grid so that you can access it later
You can see all of the client side events for the UltraWebGrid in the online documentation:
http://help.infragistics.com/doc/ASPNET/2011.1/CLR4.0/?page=WebGrid_Client_Side_Events_CSOM.html
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.
i had two combobox(Catogery, product) in my grid.
what i want to do is.
if i select category it will display some data set value based on category,
and if i select product it will bind some values in that datagrid cells.like price quantity..
i don't have idea about how to write event for combo box selected index change event which is inside the grid.
Handle the CellEndEdit event on the DataGridView. You'll have to test that the sender is the correct DataGridViewComboBoxColumn, read the value, and use that to bind the other DataGridViewComboBoxColumn to the appropriate data source.
I'm not sure about that, because I don't have visual studio at the moment. But there must be an event called RowCellValueChanged event, write your code in this event, hand row index with EventArgs e and set your values to row which you have an index of row.
Okay big brains here's something that's more of a challenge than a requirement. I am a bit stumped. I usually just need a prod in the right direction, so get your prodding sticks ready.
I have a tabcontrol covered in textboxes. I want to perform a check of the contents of all the textboxes during the SelectedIndexChanged event on a listview on the same form. If one of the textboxes has data different from a DataTable row - represented by the ListView Item - I want it to ask if the user would like to keep the change they just made. If nothing has changed I want it to just change the selection.
So obviously I'm comparing the contents of the text boxes against associated columns in the datarow.
I could just brute force the check and do each individual check one at a time. I'd prefer to come up with some clever algorithmic way of cycling through the tabcontrol textboxes and checking the values against the columnar values.
Any suggestions?
EDIT: I like the "cleverly named textboxes" solution below best, although both are good. If no one else has a better idea in the next 14 days the textbox answer gets the green.
Give the textboxes a clever name as in a portion of the name is the column/row name.
Group the textbox controls an loop through them. For each control, get the (portion)name and use that as a reference to your datatable. Check the values.
If I'm understanding you right, you want to avoid comparing every textbox on every change, in favour of just checking the textboxes that are changed, driven by the SelectedIndexChanged event of the ListView control. Is that right?
Well, DataRows and DataTables already have row versioning and rollbacks implemented, so if you bind the text boxes to the underlying row (either by writing events to write back on change/lose focus or by using an automated mechanism to accomplish the same task), then check the RowState property on SelectedIndexChanged. If the RowState is anything other than unchanged, prompt the user to save. If he saves, commit the changes, otherwise reject them.
So, for example, you'd want something like this in your SelectedIndexChanged event handler:
if (row.RowState == DataRowState.Modified) {
// prompt for user input
if (promptResult == PromptResult.Save) {
row.AcceptChanges();
}
else {
row.RejectChanges();
}
}