How to access 2 datakeynames in a GridView with OnRowCommand

Last post 09-19-2008 10:47 AM by Xaisoft. 9 replies.

Sort Posts:

  • How to access 2 datakeynames in a GridView with OnRowCommand

    09-18-2008, 3:10 PM
    • Member
      42 point Member
    • Xaisoft
    • Member since 04-15-2008, 5:42 PM
    • Posts 64

    I have a gridview with two data keys.  I can access one datakey fine via the CommanArgument of the GridView, but I am unsure of how to access two data keys?

     

    Thanks,

    X

  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-18-2008, 3:59 PM

     Well, here's a snippet where I'm getting them using the selected row. Does this help?

        Protected Sub GridView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
            Dim docid As Int32 = GridView1.SelectedDataKey.Values().Item("FDP_FILE_ID")

            Dim filename As String = GridView1.SelectedDataKey.Values().Item("FDP_FILE_NAME")
     

    Please Mark As Answer posts that helped you.

    "If we learn from our mistakes, I should be brilliant by now."


    MCTS - .NET Framework 2.0: Web Applications
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 9:32 AM
    • Member
      42 point Member
    • Xaisoft
    • Member since 04-15-2008, 5:42 PM
    • Posts 64
    I tried this and it doesn't even go into my SelectedIndexChanged Event for the GridView.
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 9:44 AM

    Please clarify what you mean, or post your code. You do need to change your data types, set the data key name, and so on.

    Please Mark As Answer posts that helped you.

    "If we learn from our mistakes, I should be brilliant by now."


    MCTS - .NET Framework 2.0: Web Applications
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 9:53 AM

    Xaisoft:

    I have a gridview with two data keys.  I can access one datakey fine via the CommanArgument of the GridView, but I am unsure of how to access two data keys?

     

    below is a sample code

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <style type="text/css">
            html
            {
                background-color:silver;
            }
            .content
            {
                width:600px;
                margin:auto;
                background-color:white;
            }
            .column
            {
                float:left;
                padding:10px;
                width:265px;
            }
            .column td,.column th
            {
                padding:5px;
                font:14px Georgia, Serif
            }
            .selectedRow
            {
                background-color:yellow;
            }
        </style>
        <title>Select Data Keys</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div class="content">
        <div class="column">
       
        <asp:GridView
            id="grdEmployees"
            DataSourceID="srcEmployees"
            DataKeyNames="LastName,FirstName"
            AutoGenerateSelectButton="true"
            SelectedRowStyle-CssClass="selectedRow"
            Runat="server" />
           
        </div>
        <div class="column">
           
        <asp:DetailsView
            id="dtlEmployees"
            DataSourceID="srcEmployeeDetails"
            Runat="server" />
       
        </div>

        <br style="clear:both" />
        </div>
           
        <asp:SqlDataSource
            id="srcEmployees"
            ConnectionString="<%$ ConnectionStrings:Employees %>"
            SelectCommand="SELECT LastName,FirstName
                FROM Employees"
            Runat="server" />   
           
        <asp:SqlDataSource
            id="srcEmployeeDetails"
            ConnectionString="<%$ ConnectionStrings:Employees %>"
            SelectCommand="SELECT * FROM Employees
                WHERE FirstName=@FirstName AND LastName=@LastName"
            Runat="server">
            <SelectParameters>
            <asp:ControlParameter
                Name="FirstName"
                ControlID="grdEmployees"
                PropertyName='SelectedDataKey["FirstName"]' />  
            <asp:ControlParameter
                Name="LastName"
                ControlID="grdEmployees"
                PropertyName='SelectedDataKey["LastName"]' />  
            </SelectParameters>
         </asp:SqlDataSource>  
       
        </form>
    </body>
    </html>

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.

    http://www.onlinepersonaldairy.com. A site to have personal details online. Enjoy.
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 9:57 AM
    Answer

    Xaisoft:

    I have a gridview with two data keys.  I can access one datakey fine via the CommanArgument of the GridView, but I am unsure of how to access two data keys?

     

    Thanks,

    X


    in those situations, we will specify the command argument as

    CommandARgument = '<%# Container.DisplayIndex %>'

    which will give the rowno. once we get rowno, we can access all

    now in rowcommand event,

    int rowno = Convert.TOInt32(e.CommandArgument);

    string col1 = gridview1.datakeys[rowno]["column1"].tostring()

    string col2 = gridview1.datakeys[rowno]["column2"].tostring() 

     

     

     

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 10:10 AM
    • Member
      42 point Member
    • Xaisoft
    • Member since 04-15-2008, 5:42 PM
    • Posts 64

    Thanks,

    That worked:  Here is the code that I ended up doing for my OnRowCommand of my gridview:

     

     protected void gvListings_OnRowCommand(object sender, GridViewCommandEventArgs e)
            {
                int rownum = Convert.ToInt32(e.CommandArgument);
                string FRN = gvListings.DataKeys[rownum]["FRN"].ToString();
                string BusinessName = gvListings.DataKeys[rownum]["BusinessName"].ToString();
    
                switch (e.CommandName)
                {
                    case "ViewDetails":
                        this.FRN = FRN;
                        this.BusinessName = BusinessName;
                        this.gvLeases.Visible = true;
                        this.gvLeases.DataBind();
                        this.updPnlListingDetail.Update();
                        this.mdlPopup.Show();
                        break;
                    case "ViewContact":
                        BusinessId = Convert.ToInt32(e.CommandArgument);
                        this.dvContact.Visible = true;
                        this.dvContact.DataBind();
                        this.updPnlContact.Update();
                        this.mdlPopupContact.Show();
                        break;
                }
            }
      
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 10:23 AM
    • Member
      42 point Member
    • Xaisoft
    • Member since 04-15-2008, 5:42 PM
    • Posts 64

    A followup question.  Since I am accessing it through the rownum, do I still need to use DataKeyNames in my GridView.  Here is my code:

    <asp:GridView ID="gvListings" runat="server" OnRowDataBound="RowDataBound" AutoGenerateColumns="False" AllowPaging="True" 
    AllowSorting="True" CssClass="datatable" CellPadding="0" BorderWidth="0px" GridLines="Vertical" DataKeyNames="FRN,BusinessName" OnRowCommand="gvListings_OnRowCommand">
      
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 10:25 AM

     definitely. you are accessing them only.

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: How to access 2 datakeynames in a GridView with OnRowCommand

    09-19-2008, 10:47 AM
    • Member
      42 point Member
    • Xaisoft
    • Member since 04-15-2008, 5:42 PM
    • Posts 64

    Yep.Thanks again.  I just realized that it was a dumb question when I realized that I am actually accessing DataKeys.

    Thanks again,

    X

Page 1 of 1 (10 items)