Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving bound data from a Gridview

I have a Gridview connected to a SQL datasource displaying data, and what I hope to do is retrieve data associated with a selected row through a button and Eval.

Somthing like,

<asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />

But I can't call Eval from code, and I don't know how to get the DataKey either.

I've been scouring the internet, but haven't found anything nice. Can anyone help me? Would be much appreciated.

EDIT:

<asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False"
DataKeyNames="ScheduleID" DataSourceID="SqlDataSource1">
    <Colums>
        <asp:TemplateField HeaderText="Date" SortExpression="Date">
           <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Date")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Time" SortExpression="starttime">
           <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("starttime")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
           <ItemTemplate>
            <asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />
          </ItemTemplate>
        </asp:TemplateField>
    </Colums>
</asp:GridView>

It's skimmed, but that's basically it.

like image 375
Quinson Avatar asked Aug 09 '12 05:08

Quinson


1 Answers

May this help

Edit your gridview html <Colums> to </Columns>

<asp:LinkButton runat=server OnClientClick="RetrieveInfo"   CommandName="Update"  Text="Send" />

Gridview Html

<asp:GridView ID="GridView1" Width="50%" runat="server" AutoGenerateColumns="False" 
            onrowcommand="GridView1_RowCommand" >
   <Columns>
        <asp:TemplateField HeaderText="Date" SortExpression="Date">
           <ItemTemplate>
            <asp:Label ID="lbldate" runat="server" Text='<%#Eval("Date")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Time" SortExpression="starttime">
           <ItemTemplate>
            <asp:Label ID="lbltime" runat="server" Text='<%#Eval("starttime","{0:dd MMM yyyy}") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
           <ItemTemplate>
            <asp:LinkButton runat="server" CommandName="sendvalue" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClientClick="RetrieveInfo" Text="Send" />
          </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code Behind:

     protected void Page_Load(object sender, EventArgs e)
       {
           bindGridview();
       }

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "sendvalue")
            {
                int getrow = Convert.ToInt32(e.CommandArgument);
                Label lbldate = (Label)GridView1.Rows[getrow].FindControl("lbldate");
                Label lbltime = (Label)GridView1.Rows[getrow].FindControl("lbltime");
                string getDate = lbldate.Text;
                string getStartTime = lbltime.Text;
               //here you retrieve all the value of select row and do your logic for link butn
                GridView1.EditIndex = -1;
                bindGridview();
            }
    }

    public void bindGridview()
    {
        SqlDataAdapter dap = new SqlDataAdapter("select Date,startTime from yourtable", con);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        DataTable dt = ds.Tables[0];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
like image 170
Satinder singh Avatar answered Sep 28 '22 02:09

Satinder singh