Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Duplicate Row in Datagrid

Hi there,
I would like to duplicate the active Row of my Datagrid. Background: Users have been working with Excel for input Data, now I am developing new Data input application in .NET. So they have been able to copy rows/columns in Excel (right mouse functions).

I have to offer them any alike function to be abls to save some typing.

So I want to realize a button that can be clicked and the active row is inserted as a copy into the datagrid.

I tried it like that:
- defined global Array
- putting event handler on event currentCellChanged in Datagrid
- reading all columns of current Cell and putting the values into the array
- on Button Click inserting the Array values into a new DataRow and putting this DataRow into DataTable of the Dataset

My Problem: If I insert the Array Values into the Dataset.Datatable I get an error because it is tried to insert the first value into the Database Columnd that coutains the primays key (auto value). This value should be ignored!

Could anyone please help me realizing this issue?
0
MPKR
Asked:
MPKR
  • 12
  • 12
1 Solution
 
RonaldBiemansCommented:
make the column that holds the primary key empty
0
 
MPKRAuthor Commented:
Good idea... but how?

I use:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr As DataRow
        dr = ds_Detail.Tables(0).NewRow()
        dr.ItemArray = arrRowData
        ds_Detail.Tables(0).Rows.Add(dr)
   End Sub
0
 
RonaldBiemansCommented:
Give me a second, I'll try to work something out

maybe it is as simple as (I don't know if this works I haven't tested it yet)

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr As DataRow
        dr = ds_Detail.Tables(0).NewRow()
        arrRowdata(0) = dbnull.value
        dr.ItemArray = arrRowData
        ds_Detail.Tables(0).Rows.Add(dr)
   End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MPKRAuthor Commented:
I think this would overwrite the first stored value inside my array, right?

By the way: I a not showing all columns from my dataset.datatable inside the Datagrid. So I have the problem of assigning the correct array value to the correct column.

Perhaps the use of array is not the best way to go here??
0
 
RonaldBiemansCommented:
How are  you copying the data to the array, could you post that code
0
 
MPKRAuthor Commented:
First:  arrRowdata(0) = dbnull.value does not work. Error: Column 'RMeld_Data_id' does not allow Null Values

I think it must be totaly ignored, so I have increased i var as marked below to start at ordinal 1. Then it works, a nwe row is inserted into DB, but other all fields are set to NULL ow.

Complete code:

    Dim arrRowData(10)
    Private Sub dg_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim currentDG As DataGrid
        Dim currentRow, iColumns As Integer
        currentDG = sender
        currentRow = currentDG.CurrentRowIndex
        If currentRow < 0 Then Exit Sub
        iColumns = currentDG.VisibleColumnCount
        Debug.WriteLine("iCols: " & iColumns)
        For i As Integer = 1 To iColumns - 1                                            'i increased to 1
            Debug.WriteLine(currentDG.Item(currentRow, i))
            arrRowData(i) = currentDG.Item(currentRow, i)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr As DataRow
        dr = ds_Detail.Tables(0).NewRow()
'        arrRowData(0) = DBNull.Value => not working
        dr.ItemArray = arrRowData
        ds_Detail.Tables(0).Rows.Add(dr)
    End Sub
0
 
RonaldBiemansCommented:
Just give me a couple of minutes I'll work something out
0
 
RonaldBiemansCommented:
I think you have to go with something like this

dim dr as datarow

Private Sub dg_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
         Dim bm As BindingManagerBase = Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
        If bm.Position < 0 Then Exit Sub
        dr = CType(bm.Current, DataRowView).Row
    End Sub
   
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr2 As DataRow
        dr2 = ds.Tables(0).NewRow
        For x As Integer = 1 To ds_Detail.Tables(0).Columns.Count - 1
            dr2.Item(x) = dr.Item(x)
        Next
       ds_Detail.Tables(0).Rows.Add(dr2)
  End Sub
0
 
MPKRAuthor Commented:
I don't yet really understand how, but THIS WORKS fine.

Thanks alot !!!!
0
 
RonaldBiemansCommented:
Want don't you understand, I will be happy to explain
0
 
MPKRAuthor Commented:
After loading my Datagrid the code works.
Then I can copy the selected row a few times.

But Sometimes I get an error: 'System.Data.RowNotInTableException', especially after Saving the Dataset Changes after deleting some rows with the update() method.

Error:
additopnal Inform.: This Row was removed from the Table and does not contain any Data  
BeginEdit() makes it possible to create new data inside this row.

What does that mean?

0
 
RonaldBiemansCommented:
Well, I think this happens because when you delete a row the currentcell changed event doesn't fire, so there will be a reference to a row that doesn't exist anymore, If you then press the button it will try to copy that deleted row, which will throw an error.  What you could do is check the rowstate of the row you are trying to copy. Like

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       If dr.RowState = DataRowState.Deleted Then Exit Sub

        Dim dr2 As DataRow
        dr2 = ds.Tables(0).NewRow
        For x As Integer = 1 To ds_Detail.Tables(0).Columns.Count - 1
            dr2.Item(x) = dr.Item(x)
        Next
       ds_Detail.Tables(0).Rows.Add(dr2)
  End Sub
   
0
 
MPKRAuthor Commented:
Thats it.

One more question to this topic:

How to change code if I want to delete the selected Row instead of adding one?

Thanks
MP
0
 
RonaldBiemansCommented:
Dim bm As BindingManagerBase = Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
        If bm.Position < 0 Then Exit Sub
        CType(bm.Current, DataRowView).Row.Delete()
0
 
MPKRAuthor Commented:
Thank you alot !!!
0
 
RonaldBiemansCommented:
Glad I could help :-)
0
 
MPKRAuthor Commented:
Hello it's me again  :)

How can I find out if I have a Current wor that can be copied (not on a new insert row that can't be copied).

How can I change the currentRow when User clicks with RIGHT Mouse on a row and I open a contextmenu (contextmenu already works, but right mouse does not select current row that was clicked).

Thank you.
0
 
RonaldBiemansCommented:
It don't quite understand what you mean in your first question.

For the second question you have to detach the contextmenu from the datagrid and show it in code instead, in the mouseup or mousedown event

like

    Private Sub datagrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagrid1.MouseUp
        Dim pt = New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = datagrid1.HitTest(pt)
        If hti.Type = DataGrid.HitTestType.Cell Then
            datagrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)
            datagrid1.Select(hti.Row)
        End If
        If e.Button = MouseButtons.Right Then
            ContextMenu1.Show(datagrid1, pt)
        End If
    End Sub
0
 
MPKRAuthor Commented:
Thanks concerning the second part of my question.

For the first part: What I meant is: If I select the INSERT ROW of the Datagrid (the one that is not yet in the database) and push my btn to copy this row I get an exception. I do not know how I can find out if I have a valid row selected that can be copied.
0
 
RonaldBiemansCommented:
do you want to copy it, or do you want to restrict anyone copying it ? (what is the exception you get ?)
0
 
MPKRAuthor Commented:
Usually I want to make it possible to copy them.

The Exception does not raise always, but after clicking a few times on the insert row (to copy it):

"Exception of Type 'System.Data.RowNotInTableException' ist in system.data.dll
Informationen: This Row was removed frm Table and does not contain data.  BeginEdit() makes it possible to create new data in this Row."
0
 
MPKRAuthor Commented:
What I want:

Make users beeing able to copy the already filled in rows of the dg.
=> working!

Problem: I get an Execption (see above) if the copied row is the row that is by default available to insert Data into the dg. When trying to copy THIS row (a few times/twice) the exception comes up.

I think its because there is not nye a primary key available for that row...??

------------------
I don't really understand you code part:

       If hti.Type = DataGrid.HitTestType.Cell Then
            datagrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)
            datagrid1.Select(hti.Row)
        End If

Doesn't that just select the clicked Row as currentRow?I fear that does not help much, because the clicked row does automatically ger the current row, doesn't it?
0
 
RonaldBiemansCommented:
One question at a time please :-)

First is probably as you said, but you could overcome that by setting the Identityseed of the primary key (to -1) yourself.

second: your question was 'How can I change the currentRow when User clicks with RIGHT Mouse on a row and I open a contextmenu (contextmenu already works, but right mouse does not select current row that was clicked).'

my code does that

0
 
MPKRAuthor Commented:
Oh now I understand.

Thanks so far, now everything works fine.

MP
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now