Drone
Drone

Reputation: 181

retrieve data by clicking on a gridview cell in asp.net using c#

I have a gridview

<asp:GridView ID="wbsdataGV1" runat="server" Width="790px" AutoGenerateColumns="False"
    OnSelectedIndexChanged="wbsdataGV1_SelectionChanged" DataKeyNames="WBSCode">
    <Columns>

    <asp:TemplateField HeaderText="WBS Code">
         <ItemTemplate>
              <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("WBSCode") %>'></asp:LinkButton>
         </ItemTemplate>
         <ControlStyle Width="100px" />
         <ItemStyle BorderStyle="Solid" BorderWidth="1px" />
         </asp:TemplateField>
    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
         <asp:TextBox ID="TextBox40" runat="server" Text='<%# Eval("Description") %>' ReadOnly="true"
                                                    Width="300px">
          </asp:TextBox>
      </ItemTemplate>
      <ControlStyle Width="300px" />
      <ItemStyle BorderStyle="Solid" BorderWidth="1px" />
   </asp:TemplateField>
   <asp:TemplateField HeaderText="Territory Code">
       <ItemTemplate>
          <asp:Label ID="Label45" runat="server" Text='<%# Eval("TerritoryCode") %>'></asp:Label>
       </ItemTemplate>
       <ControlStyle Width="100px" />
       <ItemStyle BorderStyle="Solid" BorderWidth="1px" />
   </asp:TemplateField>
   <asp:TemplateField HeaderText="Amount">
      <ItemTemplate>
           <asp:Label ID="Label46" runat="server" Text='<%# Eval("AmountReleased") %>'></asp:Label>
      </ItemTemplate>
      <ControlStyle Width="100px" />
      <ItemStyle BorderStyle="Solid" BorderWidth="1px" />
   </asp:TemplateField>
   </Columns>
   <HeaderStyle BackColor="Wheat" BorderStyle="Solid" BorderWidth="1px" />
   <RowStyle BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" 
</asp:GridView>

now in the link button i want to click it and retrieve that row's data in the same page and show it...

i am using

protected void wbsdataGV1_SelectionChanged(object sender, EventArgs e)
{
    string rowID = (string)this.wbsdataGV1.SelectedDataKey.Value;
    con.Open();
    SqlCommand cmd = new SqlCommand(" select WBSCode,Description,TerritoryCode,AmountReleased from WBS where WBSCode='"+rowID+"'");
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        wbssearchdata1.DataSource = dt;
        wbssearchdata1.DataBind();
    }

    con.Close();
}

this code but its not firing on the click .. so how can i solve this.. any help?

Upvotes: 0

Views: 4723

Answers (1)

codingbiz
codingbiz

Reputation: 26406

From your code I couldn't see anywhere you specified the button that fires the SelectedIndexChanged event. You'll need to do these

<asp:GridView OnSelectedIndexChanged="wbsdataGV1_SelectionChanged"  
              AutoGenerateSelectButton="true" />

Or

<asp:GridView OnSelectedIndexChanged="wbsdataGV1_SelectionChanged">
<Columns>
   <asp:CommandField ShowSelectButton="true" />
</Columns>
</asp:GridView>

Or

   <asp:GridView OnSelectedIndexChanged="wbsdataGV1_SelectionChanged">
   <Columns>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="Selecting" CommandName="Select" Text="Select Row"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
   </Columns>
   </asp:GridView>

Upvotes: 1

Related Questions