Still new to WinGrid. Can someone point me to a code example that will accomplish this task? I'm trying to populate text boxes based on a selected UltraGrid row.
Thanks,
-Todd
Hi Todd,
There are two basic approaches you could take to this.
One would be to bind the TextBoxes to the same DataSource as the grid.
The other would be to manually copy the contents of the active row of the grid into the TextBoxes.
The first approach is less code, but less control. The second it more coding, but more control. Which one do think would suit the needs of your application?
I've got about 40 columns in my UltraGrid row. When a user clicks on a particular row I want to populate 40 corresponding text boxes on the other side of the form. I'm not 100% sure which of the two choices I should use. Probably would want to go with the second choice, but I suppose I should look at both. Does that sound correct?
It really depends on what your goal is and the needs of your application.
If you want to read the data out of the ActiveRow in the grid and assign it to the Textbox manually, then that's easy enough. Something like this:
void ultraGrid1_AfterRowActivate(object sender, EventArgs e) { UltraGridRow row = this.ultraGrid1.ActiveRow; if (row != null) { this.txtFirstName.Text = row.Cells["First Name"].Text; this.txtLastName.Text = row.Cells["Last Name"].Text; } else { this.txtFirstName.Text = string.Empty; this.txtLastName.Text = string.Empty; } }
Thanks Mike for your fast response to my question. I failed to mention I'm coding in VB.Net. I took the above code and plugged it into my program.
Private Sub ultragridEmp_AfterRowActivate(sender As Object, e As System.EventArgs) Handles ultragridEmp.AfterRowActivate MsgBox("inside ultragridEmp_AfterRowActivate()") Dim row As UltraGridRow = Me.ultragridEmp.ActiveRow If (row Is Nothing) Then txbxEmpFirstName.Text = row.Cells("First Name").Text txbxEmpLastName.Text = row.Cells("Last Name").Text Else txbxEmpFirstName.Text = String.Empty txbxEmpLastName.Text = String.Empty End If End Sub
When running my program I end up with two issues: First, when populating the grid from the DB, the above subroutine fires as each row is added. Second, I'm not getting any values loading into row when executing "Dim row As UltraGridRow = Me.ultragridEmp.ActiveRow". Every time it hits the "if" statement it goes right to the "else" portion. Any suggestions?
Thanks again,
Oooops! I had to change the if statement to "If (row IsNot Nothing) Then"
But now I'm getting an error on line: "txbxEmpFirstName.Text = row.Cells("First Name").Text"
Here's the exception details:
System.ArgumentException was unhandled by user code Message=Key not foundParameter name: key ParamName=key Source=Infragistics2.Shared.v12.1 StackTrace: at Infragistics.Shared.KeyedSubObjectsCollectionBase.GetItem(String key) at Infragistics.Win.UltraWinGrid.ColumnsCollection.get_Item(String key) at Infragistics.Win.UltraWinGrid.CellsCollection.GetItem(String key) at Infragistics.Win.UltraWinGrid.CellsCollection.get_Item(String key) at iAssist.formEmployee.ultragridEmp_AfterRowActivate(Object sender, EventArgs e) in C:\RRI\MADLab_Collection\Riverside_Internal\iASSIST\iAssist\01 Employee\Employee.vb:line 271 at Infragistics.Win.UltraWinGrid.UltraGrid.OnAfterRowActivate() at Infragistics.Win.UltraWinGrid.UltraGrid.FireEvent(GridEventIds id, EventArgs e) at Infragistics.Win.UltraWinGrid.UltraGrid.OnActiveRowChange(UltraGridRow newActiveRow, Boolean scrollIntoView) at Infragistics.Win.UltraWinGrid.UltraGridBase.SetActiveRow(UltraGridRow row, Boolean scrollIntoView) at Infragistics.Win.UltraWinGrid.UltraGrid.EnsureTempActiveRowAssigned() at Infragistics.Win.UltraWinGrid.UltraGridBase.PaintTimer_Tick(Object source, EventArgs e) at Infragistics.Win.UltraWinGrid.RowScrollRegion.set_FirstRow(UltraGridRow value) at Infragistics.Win.UltraWinGrid.UltraGridLayout.ClearMainUIElement() at Infragistics.Win.UltraWinGrid.UltraGridBase.Set_ListManager(Object newDataSource, String newDataMember) at Infragistics.Win.UltraWinGrid.UltraGridBase.BindingListChanged(Object sender, ListChangedEventArgs e) InnerException:
Does any of this make any sense to you? I'm probably missing something simple.
I used "First Name" as an example. You need to use the key of an actual column in your grid.
Thanks again Mike,Sorry for this dumb question; is the key the column name "First Name" I used when creating the UltraGrid column?These are the lines of code I used to create the grid columns earlier in my code: Dim col1 As DataColumn = New DataColumn("First Name", GetType(System.String)) Dim col2 As DataColumn = New DataColumn("Last Name", GetType(System.String))
Also, when initially loading the grid with rows, how do I prevent the "ultragridEmp_AfterRowActivate" event from firing on each row? I wasn't expecting it to fire until a user clicks on a row in the grid.
Thanks again. I really appreciate you help getting through this.
HI Mike,
I current have binded textbox to same dataset as the ultragrid.
The problem i am facing is that in a particular scenario, the data does not update.
Scenario 1:
1. Select the row you want to edit
2. Edit the data in textbox.
3. select the already selected row
4. Close the form. (closing of form has the logic to save the changes in the dataset to db)
Though the dataset shows that it has the changes made, but the same does not go to the db.
Any idea?
todd45040 said:Finally got it figured out. I changed the event from AfterRowActivate to ClickCell.
The problem with this is that if the user changes the active row without clicking (by using the keyboard), it won't fire.
I don't understand why you are having a problem with AfterRowActivate. The row with an index of -1 may be the FilterRow or the TemplateAddRow. But either way, it shouldn't matter, you can just skip that row.
As for why the event fires when you are adding your data initially, I'd have to see the code where you are adding the data to know what's happening. It sounds to me like you are adding the data into the grid instead of into the DataSource. Or you are using AddNew instead of Add on your DataSource. But since I don't even know what kind of DataSource you are using, it's hard for me to guess.
Finally got it figured out. I changed the event from AfterRowActivate to ClickCell. Then changed how I instanciated the row:
Dim row As UltraGridRow = e.Cell.Row
I also changed my if statement to also check to see if the row count is greater than zero:
If (row IsNot Nothing AndAlso row.Cells.Count > 0) Then
Bottom line, it's working!
Thanks again for you help Mike,
Mike,
It was looking at the first row which had an index of -1. I think that’s the header row or something. I changed the code to avoid row -1.
Private Sub ultragridEmp_AfterRowActivate(sender As Object, e As System.EventArgs) Handles ultragridEmp.AfterRowActivate 'MsgBox("inside ultragridEmp_AfterRowActivate()") Dim row As UltraGridRow = Me.ultragridEmp.ActiveRow If (row IsNot Nothing AndAlso row.Index > -1) Then txbxEmpFirstName.Text = row.Cells("First Name").Text txbxEmpLastName.Text = row.Cells("Last Name").Text Else txbxEmpFirstName.Text = String.Empty txbxEmpLastName.Text = String.Empty End If End Sub
I'm going to do a little further checking, but I think this solved my problem.