This is a migrated thread and some comments may be shown as answers.

Dynamically creating empty rows in a grid

4 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Acadia
Top achievements
Rank 1
Iron
Acadia asked on 11 Feb 2009, 02:03 PM
This problem is somewhat complex and has plagued me for quite some time now.  I need a solution if possible.

I have a grid in a rad window where the user can click a Create New Row button, and clicking that button causes a post back to a method that retrieves all current rows from the grid and adds them to a dataset, then creates a new row and adds that to the dataset as well.  Then the code binds the dataset to the grid and the grid now has the original rows as well as the new row. 

The user can add as many new rows as they want, populate the rows, then save the grid.  This all works fine, however there is one crucial piece that is not working properly and I cannot for the life of me figure out why.

I am trying to set certain rows into edit mode (based on the value of one of the fields) after the grid is bound to the dataset containing the original and new rows.  If I try to do this the rows only turn yellow and do not open into edit mode.  The code is identifying the rows propwerly based on the value, but it doesn't open them in edit mode for some reason.  Here is the code I am using:

 

'loop through datagrid and set rows with an empty job number to edit mode

 

 

For Each gridDataItem As GridDataItem In rgS.MasterTableView.Items

 

 

    Dim lblJobNum As Label = CType(gridDataItem.FindControl("lblJobNum"), Label)

 

 

    If (lblJobNum.Text = "") Then

 

        gridDataItem.Edit =

True

 

 

    End If

 

 

Next

If I omit this loop the data grid will look fine except the rows will not be in   
edit mode. However, I can click the image button and set them to edit mode   
manually but I don't want the user to have to do this.  Is there something   
wrong with how I am setting these rows to edit mode? 

 

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Feb 2009, 03:21 PM
Hello Acadia,

Try rebinding the grid as shown below:
cs:
    Protected Sub RadGrid2_PreRender(ByVal sender As Object, ByVal e As EventArgs) 
         
         For Each gridDataItem As GridDataItem In rgS.MasterTableView.Items 
             
             Dim lblJobNum As Label = DirectCast(gridDataItem.FindControl("lblJobNum "), Label) 
             If lblJobNum.Text = "" Then 
                 gridDataItem.Edit = True 
                 
             End If 
         Next 
         RadGrid2.MasterTableView.Rebind() 
     End Sub 
 
 

Thanks
Princy
0
Acadia
Top achievements
Rank 1
Iron
answered on 11 Feb 2009, 03:30 PM
Hi Princy, thanks for the suggestion.  However I tried this and it throws an exception because lblJobNum = nothing in the pre-render
0
Acadia
Top achievements
Rank 1
Iron
answered on 11 Feb 2009, 06:15 PM
I think it might be useful if I post the whole block of code causing this problem.  This has been eating up hours of my time.  The intention of this block of code is that when the user clicks the cancel button of a new or exisitng row, this code is hit, and the exisitng rows from the grid, EXCEPT FOR THE CANCELLED ROW, are placed into a hash table, which then populates a dataset and then the grid is rebound to that datset.

The problem is that although the code is running as expected and the row does not show up in the dataset, after I bind the dataset to the grid and return to the screen, that row I just cancelled is STILL THERE.  It isn't in edit mode, it's just yellow but I cannot figure out how it is persisting in the grid when it isn't in the dataset.

I have double-checked and the row that was cancelled is being removed from the dataset.  How can this be?  Please let me know if there is something special I have to do with the binding of the grid to have that row not show up.

Here is the entire block of code being called:

 

If (e.CommandName = "Cancel") Then

 

 

gridTransAmtSum = 0

 

Dim transSeqCntrl As Label = CType(e.Item.FindControl("lblTransSeq"), Label)

 

 

Dim seq As Double = CType(transSeqCntrl.Text, Double)

 

 

Dim myDs As New DataSet

 

 

' Add DataTable to DataSet

 

myDs.Tables.Add(createNewSubsDataTable)

 

'Fill the dataset with what's already in the table

 

 

For Each gridItem As GridItem In rgS.MasterTableView.Items

 

 

'Prepare new row to be added to the DataSource

 

 

Dim newRow As DataRow

 

newRow = myDs.Tables(0).NewRow

 

'Insert new values

 

 

Dim newSubTValues1 As Hashtable = New Hashtable

 

 

 

If (Not gridItem.IsInEditMode) Then

 

 

 

'Grid row IS NOT in edit mode

 

 

Dim lblRecNum As Label = CType(gridItem.FindControl("lblRecNum"), Label)

 

 

Dim lblTransSeq As Label = CType(gridItem.FindControl("lblTransSeq"), Label)

 

 

Dim lblJobNum As Label = CType(gridItem.FindControl("lblJobNum"), Label)

 

 

Dim lblPhase As Label = CType(gridItem.FindControl("lblPhase"), Label)

 

 

Dim lblCategory As Label = CType(gridItem.FindControl("lblCategory"), Label)

 

 

Dim lblTransAmt As Label = CType(gridItem.FindControl("lblTransAmt"), Label)

 

 

Dim lblItemDescr As Label = CType(gridItem.FindControl("lblItemDescr"), Label)

 

 

Dim lblTransComp As Label = CType(gridItem.FindControl("lblCompCode"), Label)

 

 

Dim lblEditFlag As Label = CType(gridItem.FindControl("lblEditFlag"), Label)

 

 

If (seq <> lblTransSeq.Text) Then

 

 

If (Not lblTransSeq Is Nothing) Then

 

newSubTValues1(

"RECNUM") = lblRecNum.Text

 

newSubTValues1(

"JOBNUM") = lblJobNum.Text

 

newSubTValues1(

"PHASE") = lblPhase.Text

 

newSubTValues1(

"CATEGORY") = lblCategory.Text

 

newSubTValues1(

"TRANS_AMOUNT") = lblTransAmt.Text

 

newSubTValues1(

"ITEM_DESCR") = lblItemDescr.Text

 

newSubTValues1(

"TRANS_COMP") = lblTransComp.Text

 

newSubTValues1(

"EDIT_FLAG") = "FALSE"

 

newSubTValues1(

"SUB_TRANS_SEQ") = lblTransSeq.Text

 

 

 

End If

 

 

End If

 

 

 

 

For Each entry As DictionaryEntry In newSubTValues1

 

newRow(

CType(entry.Key, String)) = entry.Value

 

 

Next

 

myDs.Tables(0).Rows.Add(newRow)

 

 

 

 

 

Else

 

 

'Grid row IS in edit mode

 

 

Dim lblRecNum As Label = CType(gridItem.FindControl("lblRecNum"), Label)

 

 

Dim lblTransSeq As Label = CType(gridItem.FindControl("lblTransSeq"), Label)

 

 

Dim tbJobNum As TextBox = CType(gridItem.FindControl("tbJobNum"), TextBox)

 

 

Dim tbPhase As TextBox = CType(gridItem.FindControl("tbPhase"), TextBox)

 

 

Dim tbCategory As TextBox = CType(gridItem.FindControl("tbCategory"), TextBox)

 

 

Dim tbItemDescr As TextBox = CType(gridItem.FindControl("tbItemDescr"), TextBox)

 

 

Dim tbAmt As TextBox = CType(gridItem.FindControl("tbTransAmt"), TextBox)

 

 

Dim tbTransComp As TextBox = CType(gridItem.FindControl("tbCompCode"), TextBox)

 

 

Dim lblEditFlag As Label = CType(gridItem.FindControl("lblEditFlag"), Label)

 

 

If (Not lblTransSeq Is Nothing) Then

 

 

If (lblTransSeq.Text <> "") Then

 

newSubTValues1(

"RECNUM") = lblRecNum.Text

 

newSubTValues1(

"JOBNUM") = tbJobNum.Text

 

newSubTValues1(

"PHASE") = tbPhase.Text

 

newSubTValues1(

"CATEGORY") = tbCategory.Text

 

newSubTValues1(

"TRANS_AMOUNT") = tbAmt.Text

 

newSubTValues1(

"ITEM_DESCR") = tbItemDescr.Text

 

newSubTValues1(

"TRANS_COMP") = tbTransComp.Text

 

newSubTValues1(

"EDIT_FLAG") = "TRUE"

 

 

'make sure that unique primary key value is generated for the inserted row

 

newSubTValues1(

"SUB_TRANS_SEQ") = lblTransSeq.Text

 

 

End If

 

 

End If

 

 

 

 

For Each entry As DictionaryEntry In newSubTValues1

 

newRow(

CType(entry.Key, String)) = entry.Value

 

 

Next

 

 

 

myDs.Tables(0).Rows.Add(newRow)

 

 

 

End If

 

 

Next

 

 

 

myDs.AcceptChanges()

NOW THAT THE DATASET HAS ALL OF THE ROWS IN IT, NEW AND OLD, AS WELL AS THE CANCELLED ROW
HOW DO I DELETE THE CANCELLED ROW???
IF I TRY THIS:

 

'For Each dRow As DataRow In myDs.Tables(0).Rows

 

 

' If (dRow("SUB_TRANS_SEQ").ToString = seq) Then

 

 

' dRow.Delete()

 

 

' Exit For

 

 

' End If

 

 

'Next

 

 

IT DOES SOME VERY STRANGE STUFF AND DELETES THE WRONG ROWS AS WELL AS DUPLICATES A ROW.

rgS.DataSource = myDs.Tables(0)

rgS.DataBind()

 

End If

 

0
Georgi Krustev
Telerik team
answered on 16 Feb 2009, 10:26 AM
Hello Acadia,

I suppose the problem is in the change of the Row collection when it is looped. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

Here is a code snippet which I can suggest you to use in order to delete the correct row:
Dim i As Integer = 0 
While i < myDs.Tables(0).Rows.Count 
    If dRow("SUB_TRANS_SEQ").ToString = "seq" Then 
        dRow.Delete() 
        System.Math.Max(System.Threading.Interlocked.Decrement(i),i + 1) 
    End If 
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1) 
End While 

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Acadia
Top achievements
Rank 1
Iron
Answers by
Princy
Top achievements
Rank 2
Acadia
Top achievements
Rank 1
Iron
Georgi Krustev
Telerik team
Share this question
or