简体   繁体   English

如何使用c#从asp.net中的datagrid获取选定的列值或名称

[英]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 然后,您可以使用selectedRow对象并调用属于它的任何属性,即var name = selectedRow.Name

It can be possible through DataKeyNames and Normal method also 也可以通过DataKeyNames和Normal方法

1) 'e' as Commandargument 1)'e'作为指挥官

int index = Convert.ToInt32(e.CommandArgument); int索引= Convert.ToInt32(e.CommandArgument); string request = gvRequests.Rows[index].Cells[4].Text.ToString(); 字符串请求= gvRequests.Rows [index] .Cells [4] .Text.ToString();

2) GridViewRow selectedRow = gvRequests.Rows[index]; 2)GridViewRow selectedRow = gvRequests.Rows [index]; string Id = gvRequests.DataKeys[index].Value.ToString().Trim(); 字符串ID = gvRequests.DataKeys [index] .Value.ToString()。Trim();

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

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e) 受保护的void gvCustomers_RowDataBound(对象发送者,GridViewRowEventArgs e)

{ {

if ((e.Row.RowType == DataControlRowType.DataRow)) 如果((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... .. 请享用... ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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