Link to home
Start Free TrialLog in
Avatar of dshi15
dshi15Flag for United States of America

asked on

How to get key value in gridview Updating event?

I have a GridView1 related a customer table, I have customerId field. When user click on edit, they can edit first name, last name, but when they click on update, I want update first name and last name in other table related customerID field in same time, I wrote code

 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
     
' I need get which customerId user try to update, so I can update other table.
   
 End Sub

How I can do it?

Thansks.
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Try this way:

Dim myID As Integer = GridView1.Rows(e.RowIndex).Cells(0).Text

Where 0 is the number of the column (zero based)
Avatar of dshi15

ASKER

Could you write on detail?
 Because I don't have customerId show up in gridview. I just use it as key. <asp:GridView  ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" .........


Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
     
' I need get  customerId user try to update, first name so I can update other table.
   
 end sub

Thanks,
No problem!

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
      Dim myID As Integer = e.Keys.Item("CustomerID")
End Sub
Avatar of dshi15

ASKER

I tried and  it does'n work.

I have

dim CustomerID as integer, Firstname as string
CustomerID = e.Keys.Item("CustomerID")
FirstName = GridView1.Rows(e.RowIndex).Cells(0).Text

And I saw you use GridView1 and e alternativelly. What it means?  they are the same here?

Thanks,

>> I tried and  it does'n work.
What does not work ? Be more clear ... you don't get the CustomerID , the FirstName  ...
Avatar of dshi15

ASKER

No, I didn't get. CustomerId is 0, but I just went internet searched, I changed code to

CustomerId= GridView1.DataKeys(e.RowIndex).Value, it works, I don't know why?
second line still not work.
FirstName = GridView1.Rows(e.RowIndex).Cells(0).Text

Thanks,

For me it work fine e.Keys.Item("CustomerID") but GridView1.DataKeys(e.RowIndex).Value should work also.

But GridView1.Rows(e.RowIndex).Cells(0).Text will get the column 0 selected row information. Do you have the FirstName in the first column or in the second ?

Try:
GridView1.Rows(e.RowIndex).Cells(1).Text

Avatar of dshi15

ASKER

Thank,
e.Keys.Item("CustomerID")
This line worked. Becuase I used CustomerID in one place CustomerId in another. Sorry about it.

But this line GridView1.Rows(e.RowIndex).Cells(1).Text still not work.

I have my Gridview as the following. I used edit item in gridview in design view, it generates a lot of code for me.

<asp:GridView  ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
                        DataSourceID="sqlCustomer" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                            <asp:TemplateField HeaderText="Delete">
                                <EditItemTemplate>
                                    <asp:Button runat="server" Text="Delete" CommandName="Delete"></asp:Button>
                               
</EditItemTemplate>
                                <ItemTemplate>
                                   
                               
               
</ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
                                <edititemtemplate>
<asp:TextBox id="TextBox5" runat="server" Text='<%# Bind("FirstName") %>' Columns="13"></asp:TextBox>
</edititemtemplate>
                                <itemtemplate>
<asp:Label id="Label9" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</itemtemplate>
                            </asp:TemplateField>



I tried
        FirstName = GridView1.Rows(e.RowIndex).Cells(1).Text
        FirstName = GridView1.Rows(e.RowIndex).Cells(2).Text
        FirstName = GridView1.Rows(e.RowIndex).Cells(3).Text

No one has value.

thanks,



Why do you use the ItemTemplate and not the columns from the gridview ?
Avatar of dshi15

ASKER

I don't know, I will change it to bound field then test it again?

thanks.
Avatar of dshi15

ASKER

I changed to boundField, I tried
        FirstName = GridView1.Rows(e.RowIndex).Cells(1).Text
        FirstName = GridView1.Rows(e.RowIndex).Cells(2).Text
        FirstName = GridView1.Rows(e.RowIndex).Cells(3).Text

it is still not work. Any idea?
e.Keys.Item("CustomerID") or GridView1.DataKeys(e.RowIndex).Value both work.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" InsertVisible="False" ReadOnly="True"
                    SortExpression="CustomerID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />


Thanks.
Are you using the RowUpdating event ? I have tried this and it works fine. Start a new gridview and test it again.

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        MsgBox(GridView1.Rows(e.RowIndex).Cells(1).Text)
    End Sub

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            DataSourceID="AccessDataSource1" EmptyDataText="There are no data records to display.">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="myField" HeaderText="myField" SortExpression="myField" />
            </Columns>
        </asp:GridView>
Avatar of dshi15

ASKER

Thanks a lot, I will try again. We need template field because we need ajust text box width, bound field can not.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dshi15

ASKER

Thank you very much. I don't have time to test yet. but I believe you are right. After I test, it not work, I will submit another question.