Chamath Viduranga
Chamath Viduranga

Reputation: 145

Need to load data from grid view to text box and droup down using Row command

This is my Grid view. I want to know how to bind data to Text boxes and drop down list using Row command event.

 <asp:Panel ID="Panel2" runat="server"  CssClass="mid">
    <asp:GridView ID="BuyerGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="BuyerId" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="BuyerGrid_RowCommand">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="BuyerName" HeaderText="Buyer Name" />
            <asp:BoundField DataField="BuyerCode" HeaderText="Buyer Code" />
            <asp:BoundField DataField="CountryName" HeaderText="Country" />
            <asp:BoundField DataField="CityName" HeaderText="City" />
             <asp:TemplateField HeaderText="" SortExpression="">
                <ItemTemplate>


<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit"
CommandArgument='<%#Eval("BuyerId") %>'>Edit</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
            </asp:Panel>

This is my code behind part.

    protected void BuyerGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            //Bind values in the text box of the pop up control
            tbxBuyerName.Text = BuyerGrid.Rows[index].Cells[0].Text;
            tbxBuerCode.Text = BuyerGrid.Rows[index].Cells[1].Text;
            //.Text = gview.Rows[index].Cells[2].Text;
        }
    }

Upvotes: 0

Views: 320

Answers (1)

codeandcloud
codeandcloud

Reputation: 55200

Change

CommandArgument='<%#Eval("BuyerId") %>'

to

CommandArgument='<%# Container.DataItemIndex %>'

or

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

Upvotes: 1

Related Questions