Monday, April 16, 2012

How to find Datakey value on RawdataBound Event of Gridview


1.

 <asp:GridView ID="grdInvoices" runat="server"
                      DataKeyNames="InvoiceID,ClientID,CustID,SourceDesc" AutoGenerateColumns="false"
                      GridLines="None" onrowdatabound="grdInvoices_RowDataBound"
                      onrowcommand="grdInvoices_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="InvoiceDate" HeaderText ="Invoice Date" ItemStyle-Width="150" />
                        <asp:BoundField DataField="InvoiceNumber" HeaderText ="Invoice Number" ItemStyle-Width="150" />
                        <asp:BoundField DataField="InvoiceAmount" HeaderText ="Invoice Amout" ItemStyle-Width="150" />
                        <asp:BoundField DataField="TermsOfPayment" HeaderText ="Term Of Payment" ItemStyle-Width="150" />
                        <asp:templatefield headertext="Source Description">
                        <itemtemplate>
                        <asp:dropdownlist id="ddlSourceDesc" runat="server">
                        </asp:dropdownlist>
                        </itemtemplate>
                        </asp:templatefield>
                        <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="10%">
                        <ItemTemplate>
                        <asp:Button ID="btnAdd" runat="server" Text="Save" CssClass="button" CommandName="SaveSourceDes"/>
                        </ItemTemplate>

<HeaderStyle Width="10%"></HeaderStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>
                       
                    </Columns>
                    <HeaderStyle CssClass="grid_heading_left" />
                    <RowStyle CssClass="grid_row1" />
                    <AlternatingRowStyle CssClass="grid_row2" />
                </asp:GridView>


2. Reterive Datakey value
string Binbdval=grdInvoices.DataKeys[e.Row.RowIndex].Values[3].ToString();
                ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(Binbdval));

No comments:

Post a Comment