Get the cell value of a GridView row

后端 未结 8 1045
灰色年华
灰色年华 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 18:46

    I suggest you use a HiddenField inside template field use FindControl to find this field.

    ie:

    ASPX

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:HiddenField ID="hfFname" runat="server" Value='<%# Eval("FileName") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
    

    Code behind

    protected void gvAttachments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView gv1 = (GridView)sender;
            GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.RowIndex];
    
            //get hidden field value and not directly from the GridviewRow, as that will be null or empty!
            HiddenField hf1 = (HiddenField)gvr1.FindControl("hfFname");
            if (hf1 != null)
            {
               ..
            }
        }
    
    0 讨论(0)
  • 2021-01-01 18:49

    Try changing your code to

    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = dgCustomer.SelectedRow;
    
    // And you respective cell's value
    TextBox1.Text = row.Cells[1].Text
    

    UPDATE: (based on my comment) If all what you are trying to get is the primary key value for the selected row then an alternate approach is to set

    datakeynames="yourprimarykey"

    for the gridview definition which can be accessed from the code behind like below.

    TextBox1.Text = CustomersGridView.SelectedValue.ToString();
    
    0 讨论(0)
  • 2021-01-01 18:53

    Expanding on Dennis R answer above ... This will get the value based on the Heading Text (so you don't need to know what column...especially if its dynamic changing).

    Example setting a session variable on SelectedIndexChange.

        protected void gvCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            int iCustomerID = Convert.ToInt32(Library.gvGetVal(gvCustomer, "CustomerID"));
            Session[SSS.CustomerID] = iCustomerID;
        }
    
    public class Library
    {
        public static string gvGetVal(GridView gvGrid, string sHeaderText)
        {
            string sRetVal = string.Empty;
            if (gvGrid.Rows.Count > 0)
            {
                if (gvGrid.SelectedRow != null)
                {
                    GridViewRow row = gvGrid.SelectedRow;
                    int iCol = gvGetColumn(gvGrid, sHeaderText);
                    if (iCol > -1)
                        sRetVal = row.Cells[iCol].Text;
                }
            }
            return sRetVal;
        }
    
        private static int gvGetColumn(GridView gvGrid, string sHeaderText)
        {
            int iRetVal = -1;
            for (int i = 0; i < gvGrid.Columns.Count; i++)
            {
                if (gvGrid.Columns[i].HeaderText.ToLower().Trim() == sHeaderText.ToLower().Trim())
                {
                    iRetVal = i;
                }
            }
            return iRetVal;
        }
    }
    
    0 讨论(0)
  • 2021-01-01 19:01

    Windows Form Iteration Technique

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Selected)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                int index = cell.ColumnIndex;
                if (index == 0)
                {
                    value = cell.Value.ToString();
                    //do what you want with the value
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 19:04
    string id;
    foreach (GridViewRow rows in grd.Rows)
    {
       TextBox lblStrucID = (TextBox)rows.FindControl("grdtext");
       id=lblStrucID.Text
    }
    
    0 讨论(0)
  • 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:

     <asp:TemplateField HeaderText="料號">
        <ItemTemplate>
        <asp:Label ID="Part_No" runat="server" Text='<%# Eval("Part_No")%>' ></asp:Label>
        </ItemTemplate>
        <HeaderStyle CssClass="bhead" />
        <ItemStyle CssClass="bbody" />
        </asp:TemplateField>
    

    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

    0 讨论(0)
提交回复
热议问题