简体   繁体   中英

How can i hold gridview row selected values into a string in asp.net

I have gridview control it contains 6 columns when i click 6th column row of gridview i need to selecte that contains columns row text into a string. how can i take here i am taking commandargument is a string and how can i take another column names text

my code:

<asp:GridView ID="GridView1" Width="950px" CssClass="Grid" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"  >
                    <Columns>
                          <asp:GridView ID="GridView1" Width="950px" CssClass="Grid" runat="server" AutoGenerateColumns="false" >
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="" ItemStyle-ForeColor="White" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="SName" HeaderText="SName" />                        
            <asp:BoundField DataField="Date" HeaderText="Date" />
            <asp:BoundField DataField="Size" HeaderText="Size(MB)" />     
            <asp:BoundField DataField="Time" HeaderText="Time" />                 

                   <asp:TemplateField HeaderText="FileName">  
                                    <ItemTemplate>  
                                        <asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandArgument='<%# Eval("FileName") %>'  
                                            CommandName="Download" Text='<%# Eval("FileName") %>' />  
                                    </ItemTemplate>  
                                </asp:TemplateField>   
       <asp:TemplateField HeaderText="S.No." Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%#Bind("ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>                     
                    </Columns>        
                </asp:GridView>

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID1;
        if (e.CommandName == "Download")
        {
            GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int rowIndex = gvr.RowIndex;
            Label Itemid = (Label)GridView1.Rows[rowIndex].FindControl("lblID");
            ID1 = (Itemid).Text;
            Session["ID"] = ID1;
            string filename = e.CommandArgument.ToString();
//here how can i hold another column text
          }
          }

I have a easiest way to do this same thing...

  <asp:Label ID="lblName" runat="server" Text='<%#Eval("ID").ToString() +", "+ Eval("OtherCoulmn").ToString() %>'></asp:Label>

--- hope it helps  

  Also the best way is to do something like this 

 ((MyObject)Container.DataItem).MyProperty where MyObject is the Model which you bind with grid and property which you wan to use in rowcommand its clean .

Simply follow the bellow code sample:

protected void btnEdit_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    Label lblId = gvCipamMember.Rows[gvr.RowIndex].FindControl("lblPersonId") as Label;
            cmd = new SqlCommand("SELECT Id,Res_Person,Email_ID,Mobile_NO,Cipam_Flag FROM Responsibilty_Master WHERE Id=@Id", con.MyConnection);
            cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(lblId.Text.ToString()));
            dt = new DataTable();
            con.MyConnection.Open();
            dt.Load(cmd.ExecuteReader());
            if (dt.Rows.Count > 0)
            {
                txtPersonName.Text = dt.Rows[0]["Res_Person"].ToString();
                txtEmail.Text = dt.Rows[0]["Email_ID"].ToString();
                txtMobileNo.Text = dt.Rows[0]["Mobile_NO"].ToString();
                if(Convert.ToBoolean(dt.Rows[0]["Cipam_Flag"].ToString())==true)
                {
                    chkbCipamFlag.Checked = true;
                } 
             }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM