Get the cell value of a GridView row

后端 未结 8 1108
灰色年华
灰色年华 2021-01-01 18:23

I am using the GridView - AutoGenerateSelectButton = \"True\" to select the row in order to get the Column 1 cell value.

I have tried:

         


        
8条回答
  •  不知归路
    2021-01-01 19:05

    I had the same problem as yours. I found that when i use the BoundField tag in GridView to show my data. The row.Cells[1].Text is working in:

    GridViewRow row = dgCustomer.SelectedRow;
    TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
    

    But when i use TemplateField tag to show data like this:

     
        
        
        
        
        
        
    

    The row.Cells[1].Text just return null. I got stuck in this problem for a long time. I figur out recently and i want to share with someone who have the same problem my solution. Please feel free to edit this post and/or correct me.

    My Solution:

    Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label;
    

    I use Controls attribute to find the Label control which i use to show data, and you can find yours. When you find it and convert to the correct type object than you can extract text and so on. Ex:

    string showText = lbCod.Text;
    

    Reference: reference

提交回复
热议问题