Results 1 to 7 of 7

Thread: [RESOLVED] Gridview headache: Get column names when autogeneratecolumns=true?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    205

    Resolved [RESOLVED] Gridview headache: Get column names when autogeneratecolumns=true?

    I'm getting frustrated with the Gridview control. I'm populating the Gridview like this...

    VB Code:
    1. dt = obj.getDataTable("SELECT * FROM tblsamplestrings")
    2. gvTranslations.DataSource = dt
    3. gvTranslations.DataBind()

    I'm using "SELECT *" because tblsamplestrings will be variable and each table has different fields. So everything renders as expected. Now when the user clicks "Update" I need to know the column names so that I can construct an UPDATE statement. But I can't figure out how to get the column names and the column count property is ZERO (I'm guessing because the columns were autogenerated). So this is where I am stuck...

    VB Code:
    1. Protected Sub gvTranslations_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvTranslations.RowUpdating
    2.         Dim row As GridViewRow = gvTranslations.Rows(e.RowIndex)
    3.         Dim columnCount As Integer = gvTranslations.Columns.Count
    4.     End Sub

    Any help would be much appreciated.

    UDPATE: I managed to get the column names using a workaround. So now I'm stuck on the UPDATE statement.

    I have this code...

    VB Code:
    1. Protected Sub gvTranslations_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvTranslations.RowUpdating
    2.         Dim y As Integer = 0
    3.         Dim row As GridViewRow = gvTranslations.Rows(e.RowIndex)
    4.         Dim obj As New ERSLib.Data.PublicFunc
    5.         Dim sb As New StringBuilder
    6.        
    7.         sb.Append("UPDATE tblSampleStrings SET TYPE_ = '" & needNewValueHere & "'")
    8.  
    9.         obj.runSql(sb.ToString)
    10.  
    11.         obj.close()
    12.         obj = Nothing
    13.  
    14.     End Sub

    How do I fill the needNewValueHere variable with the appropriate value? I've tried everything I can think of. e.newvalues is empty. row.cells(e.rowindex) is empty. This is so frustrating
    Last edited by ferrethouse; Oct 31st, 2006 at 07:47 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    205

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    Okay. I switched to using the sqldatasource rather than binding to a datatable. That seems to work better.

    So I attached this UPDATE statement to my sqldatasource...

    SqlDataSource1.UpdateCommand = "UPDATE tblsamplestrings SET Description = escription"

    I'm connecting to Oracle from ODBC so I think the semicolon syntax is correct instead of the @ symbol. However, nothing happens. I don't get any errors but it doesn't update the description field. If I change the UPDATE statement to this...

    SqlDataSource1.UpdateCommand = "UPDATE tblsamplestrings SET Description = 'HELLOWORLD'"

    It works. So it must not be picking up the escription value. Is this not the correct syntax? In my sqldatasource I told it to automatically generate the fields.

    Aren't there any gridview experts our there???

  3. #3
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    I can post a little example of an update statement for a datagrid

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    205

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    Okay. I'm providing the complete code. I am 99% there. Someone must know why escription is holding the old value and not the new one. The TIMESTAMP field is updating correctly and the WHERE clause is working. But it is updating with the old description value not the new one that I typed in the textbox.

    HTML...
    HTML Code:
    <asp:GridView AutoGenerateEditButton=True ID="gvTranslations" runat="server" AllowPaging=True DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <EditRowStyle BackColor="#999999" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Dsn=devenvdb;uid=envdb;pwd=manager" ProviderName="System.Data.Odbc"></asp:SqlDataSource>
    Here is the vb...

    VB Code:
    1. SqlDataSource1.SelectCommand = "SELECT UNIQUEID, DESCRIPTION FROM tblsamplestrings"
    2. SqlDataSource1.UpdateCommand = "UPDATE tblsamplestrings SET DESCRIPTION = :DESCRIPTION, TIMESTAMP = CURRENT_DATE WHERE UniqueID = :UNIQUEID"

  5. #5
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    what are you setting the @description equal too???

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    205

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    Quote Originally Posted by zdavis
    what are you setting the @description equal too???
    I have to set it? I thought it was associated with the column and automatically populated.

    How do I set it? I tried moving the UpdateCommand here...

    VB Code:
    1. Protected Sub gvTranslations_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvTranslations.RowUpdating
    2. SqlDataSource1.UpdateCommand = "UPDATE tblsamplestrings SET DESCRIPTION = :DESCRIPTION, TIMESTAMP = CURRENT_DATE WHERE UniqueID = :UNIQUEID"
    3. gvTranslations.DataBind()
    4.     End Sub

    In the hopes that I could access e.NewValues but e.NewValues and e.OldValues are the same thing (they both have the value that was in that column before I hit edit and changed it). How do I get the new value that I typed into that textbox?

    Thanks.

    NOTE: I'm not setting :UNIQUEID but it is working correctly (because I don't edit it - so the old value is correct).

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    205

    Re: Gridview headache: Get column names when autogeneratecolumns=true?

    I got it to work. I had to change two things...

    1. I wasn't setting DateKeyNames property

    2. I was calling DataBind method in page_load event. It doesn't like that.

    From Microsoft...

    You must set the DataKeyNames property for the automatic updating and deleting features of the GridView control to work. The values of these key fields are passed to the data source control in order to match the row to update or delete.
    Last edited by ferrethouse; Nov 2nd, 2006 at 02:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width