Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert using a gridview with AutoGenerateColumns="true"

Posted on 2013-02-07
1
Medium Priority
?
1,960 Views
Last Modified: 2013-02-07
Hi all,

Here is my problem. I'm trying to create a dynamic gridView that can be connected to différents DataSources. To do that, I setted the AutoGenerateColumns to true and I used some command field for the delete and the edit.
Everything works fine for the update and the delete.

My next step was to manage the insert but it's impossible to have correct fields in the footer. I was thinking that .Net was able to understand that for my insert, I want the same "template" that for the edit but I tried a lot of different methods and nothing works.

Is there a method to automatically fill the footer with the "edit" template and use the automatic insert system of the gridview with AutoGenerateColumns="true" ?

I have no idea what can be in cells so for this part of the application I must auto generate columns. It's mandatory.

Maybe it's possible to copy the "edit" content of data cells to the footer or a tricks like that ?

Here is the code...

(I set the QueryName of the domainDataSource in the page init dynamically)

 
<cc1:DomainDataSource ID="dds" runat="server" 
       DomainServiceTypeName="DomainName" 
 EnableDelete="true" EnableInsert="true" EnableUpdate="true" >
</cc1:DomainDataSource>


<asp:GridView ID="grd" runat="server"
       DataSourceID="dds" AutoGenerateColumns="true"  >
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    </Columns>
</asp:GridView>

Open in new window


Thanks for the answers and I hope I'll find a solution.


------ Edit, solved

Ok, I found a solution but it's a little tricky because GridView doesn't handles Insert commands like the formviews...

So I created controls manually in the gridview creation. But because Insert event is not handled, we need to simulate the insert of the dataSource like for the update...
With a little bit of reflector, I was able to do the insert.

Here is the code that works on my project...

Creation of textbox controls and button

            
            Dim grd As GridView = DirectCast(sender, GridView)
            Dim footer As GridViewRow = grd.FooterRow
            Dim b = New Button()
            b.Text = "Insert"
            b.CommandName = "Insert"
            b.CausesValidation = True
            footer.Cells(0).Controls.Add(b)

            For index As Integer = 1 To footer.Cells.Count - 1

                Dim dcfc As DataControlFieldCell = _

                    TryCast(footer.Cells(index), DataControlFieldCell)

                If dcfc IsNot Nothing Then

                    Dim field As String = dcfc.ContainingField.ToString
                    Dim txt = New TextBox()
                    ColumnNameBinding = field
                    AddHandler txt.DataBinding, AddressOf ControlDataBinding
                    footer.Cells(index).Controls.Add(txt)

                End If

            Next

Open in new window


Method to do the Insert (to raise when the user click on insert button)

      Private Sub HandleInsert(ByVal row As GridViewRow, ByVal rowIndex As Integer, ByVal causesValidation As Boolean)

            If ((Not causesValidation OrElse (Me.Page Is Nothing)) OrElse Me.Page.IsValid) Then

                Dim data As DataSourceView = Nothing

                data = DirectCast(dds, IDataSource).GetView(String.Empty)

                Dim e As New GridViewUpdateEventArgs(rowIndex)

                Dim dataKeyNamesInternal As String() = New String() {"Key of the gridview"}

                Dim includePrimaryKey As Boolean = True

                Dim includeReadOnlyFields As Boolean = False

                'We fill the collection with the data of the gridview.
                'We must use cells because column number is 0 for autogenerateColumns="true"
		'First column is the button
                For i As Integer = 1 To row.Cells.Count - 1

                    Dim dcfc As DataControlFieldCell = TryCast(row.Cells(i), DataControlFieldCell)

                    If dcfc IsNot Nothing Then

                        Dim dictionary As New OrderedDictionary

                        Dim field As String = dcfc.ContainingField.ToString

                        'Dim dictionary As New OrderedDictionary

                        dcfc.ContainingField.ExtractValuesFromCell(dictionary, TryCast(row.Cells.Item(i), DataControlFieldCell), row.RowState, includeReadOnlyFields)

                        Dim entry As DictionaryEntry
                        For Each entry In dictionary

                            If (includePrimaryKey OrElse (System.Array.IndexOf(Of Object)(dataKeyNamesInternal, entry.Key) = -1)) Then

                                e.NewValues.Item(entry.Key) = entry.Value

                            End If
                        Next

                    End If

                Next

                If (Not e.Cancel) Then

                    data.Insert(e.NewValues, New DataSourceViewOperationCallback(AddressOf Me.HandleInsertCallback))

                End If

            End If

        End Sub

Open in new window


And just a callback function HandleInsertCallback used by data.Insert...

It's not perfect but it's working and. I'll see if I have the time to improve the code.

Regards,
0
Comment
Question by:Itec_
1 Comment
 

Accepted Solution

by:
Itec_ earned 0 total points
ID: 38864596
Ok, I found a solution but it's a little tricky because GridView doesn't handles Insert commands like the formviews...

So I created controls manually in the gridview creation. But because Insert event is not handled, we need to simulate the insert of the dataSource like for the update...
With a little bit of reflector, I was able to do the insert.

Here is the code that works on my project...

Creation of textbox controls and button

            
            Dim grd As GridView = DirectCast(sender, GridView)
            Dim footer As GridViewRow = grd.FooterRow
            Dim b = New Button()
            b.Text = "Insert"
            b.CommandName = "Insert"
            b.CausesValidation = True
            footer.Cells(0).Controls.Add(b)

            For index As Integer = 1 To footer.Cells.Count - 1

                Dim dcfc As DataControlFieldCell = _

                    TryCast(footer.Cells(index), DataControlFieldCell)

                If dcfc IsNot Nothing Then

                    Dim field As String = dcfc.ContainingField.ToString
                    Dim txt = New TextBox()
                    ColumnNameBinding = field
                    AddHandler txt.DataBinding, AddressOf ControlDataBinding
                    footer.Cells(index).Controls.Add(txt)

                End If

            Next

Open in new window


Method to do the Insert (to raise when the user click on insert button)

      Private Sub HandleInsert(ByVal row As GridViewRow, ByVal rowIndex As Integer, ByVal causesValidation As Boolean)

            If ((Not causesValidation OrElse (Me.Page Is Nothing)) OrElse Me.Page.IsValid) Then

                Dim data As DataSourceView = Nothing

                data = DirectCast(dds, IDataSource).GetView(String.Empty)

                Dim e As New GridViewUpdateEventArgs(rowIndex)

                Dim dataKeyNamesInternal As String() = New String() {"Key of the gridview"}

                Dim includePrimaryKey As Boolean = True

                Dim includeReadOnlyFields As Boolean = False

                'We fill the collection with the data of the gridview.
                'We must use cells because column number is 0 for autogenerateColumns="true"
		'First column is the button
                For i As Integer = 1 To row.Cells.Count - 1

                    Dim dcfc As DataControlFieldCell = TryCast(row.Cells(i), DataControlFieldCell)

                    If dcfc IsNot Nothing Then

                        Dim dictionary As New OrderedDictionary

                        Dim field As String = dcfc.ContainingField.ToString

                        'Dim dictionary As New OrderedDictionary

                        dcfc.ContainingField.ExtractValuesFromCell(dictionary, TryCast(row.Cells.Item(i), DataControlFieldCell), row.RowState, includeReadOnlyFields)

                        Dim entry As DictionaryEntry
                        For Each entry In dictionary

                            If (includePrimaryKey OrElse (System.Array.IndexOf(Of Object)(dataKeyNamesInternal, entry.Key) = -1)) Then

                                e.NewValues.Item(entry.Key) = entry.Value

                            End If
                        Next

                    End If

                Next

                If (Not e.Cancel) Then

                    data.Insert(e.NewValues, New DataSourceViewOperationCallback(AddressOf Me.HandleInsertCallback))

                End If

            End If

        End Sub

Open in new window


And just a callback function HandleInsertCallback used by data.Insert...

It's not perfect but it's working and. I'll see if I have the time to improve the code.

Regards,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question