Solved

Insert using a gridview with AutoGenerateColumns="true"

Posted on 2013-02-07
1
1,870 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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