简体   繁体   中英

how to get selected column value or name from datagrid in asp.net using c#

My code is like this : Now I have now idea how to get column name of selected row

protected void gv_imageslist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        string status;
        string sts;
        int result;
        lblerror2.Text = "";
        //((label)gv.Rows.FindControl("lbl1")).Text;

        string str = gv_imageslist.c

        if (str  == "Status")
        {
            status = ((Label)gv_imageslist.Rows[e.RowIndex].FindControl("lbl_Status")).Text;
            Gm.Gallery_Id = Convert.ToInt32(gv_imageslist.DataKeys[e.RowIndex].Value.ToString());
            if (status == "True")
            {
                Gm.Is_Active = false;
            }
            else
            {
                Gm.Is_Active = true;
            }
            result = Gm.Change_Image_Status();
            if (result == 1)
            {
                Build_ImageGalleryList();
            }
            else
            {
                lblerror2.Visible = true;
                lblerror2.Text = "Unable to Change Status !";
            }
        }
        else
        //------For Checking of cover pic
        {
            sts = ((Label)gv_imageslist.Rows[e.RowIndex].FindControl("lbl_Cover")).Text;
            Gm.Gallery_Id = Convert.ToInt32(gv_imageslist.DataKeys[e.RowIndex].Value.ToString());

            string sp = ((Label)gv_imageslist.Rows[e.RowIndex].FindControl("lbl_category_Id")).Text;
            Gm.Category_Id = Convert.ToInt32 (sp);
            if (sts == "False")
            {
                Gm.Is_Cover = true;
            }
            else
            {
                Gm.Is_Cover = false;
            }
            result = Gm.Change_Gallery_Cover();
            if (result == 1)
            {
                Build_ImageGalleryList();
            }
            else
            {
                lblerror2.Visible = true;
                lblerror2.Text = "Unable To Change Cover Pic !!";
            }
        }

    }

试试这个代码片段;

gv.HeaderRow.Cells[i].Text

Why not create an object of the selected row and work with it from there?

ie

var selectedRow = (TYPE)gridViewName.GetFocusedRow();

You could then use the selectedRow object and call any properties belonging to it ie var name = selectedRow.Name

It can be possible through DataKeyNames and Normal method also

1) 'e' as Commandargument

int index = Convert.ToInt32(e.CommandArgument); string request = gvRequests.Rows[index].Cells[4].Text.ToString();

2) GridViewRow selectedRow = gvRequests.Rows[index]; string Id = gvRequests.DataKeys[index].Value.ToString().Trim();

string headerText=gvProductList.HeaderRow.Cells[1].Text;

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)

{

if ((e.Row.RowType == DataControlRowType.DataRow))

{
   LinkButton lnk = (LinkButton) e.Row.FindControl("lnk");
   Label lblName= (Label) e.Row.FindControl("lblName");                      
   lnk.Attributes.Add("onclick", "getValue(" + lblName.ClientID + ");"
}

}... You can try this... method in your own way

Enjoy... ..

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