How to get the cell value by column name not by index in GridView in asp.net

后端 未结 10 1569
轻奢々
轻奢々 2020-12-01 06:21

I am having a gridview in asp.net and now I want the cell value by the column name but not by the cell index.

How would be it possible

相关标签:
10条回答
  • 2020-12-01 06:28

    It is possible to use the data field name, if not the title so easily, which solved the problem for me. For ASP.NET & VB:

    e.g. For a string:

    Dim Encoding = e.Row.DataItem("Encoding").ToString().Trim()

    e.g. For an integer:

    Dim MsgParts = Convert.ToInt32(e.Row.DataItem("CalculatedMessageParts").ToString())

    0 讨论(0)
  • 2020-12-01 06:30

    GridView does not act as column names, as that's it's datasource property to know those things.

    If you still need to know the index given a column name, then you can create a helper method to do this as the gridview Header normally contains this information.

    int GetColumnIndexByName(GridViewRow row, string columnName)
    {
        int columnIndex = 0;
        foreach (DataControlFieldCell cell in row.Cells)
        {
            if (cell.ContainingField is BoundField)
                if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
                    break;
            columnIndex++; // keep adding 1 while we don't have the correct name
        }
        return columnIndex;
    }
    

    remember that the code above will use a BoundField... then use it like:

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = GetColumnIndexByName(e.Row, "myDataField");
            string columnValue = e.Row.Cells[index].Text;
        }
    }
    

    I would strongly suggest that you use the TemplateField to have your own controls, then it's easier to grab those controls like:

    <asp:GridView ID="gv" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    and then use

    string columnValue = ((Label)e.Row.FindControl("lblName")).Text;
    
    0 讨论(0)
  • 2020-12-01 06:31
    protected void CheckedRecords(object sender, EventArgs e)
    
        {
           string email = string.Empty;
            foreach (GridViewRow gridrows in GridView1.Rows)
    
            {
    
                CheckBox chkbox = (CheckBox)gridrows.FindControl("ChkRecords");
        if (chkbox != null & chkbox.Checked)
    
                {
    
        int columnIndex = 0;
                    foreach (DataControlFieldCell cell in gridrows.Cells)
                    {
                        if (cell.ContainingField is BoundField)
                            if (((BoundField)cell.ContainingField).DataField.Equals("UserEmail"))
                                break;
                        columnIndex++; 
                    }
    
    
                    email += gridrows.Cells[columnIndex].Text + ',';
                      }
    
            }
    
            Label1.Text = "email:" + email;
    
        }                           
    
    0 讨论(0)
  • 2020-12-01 06:34
    //get the value of a gridview
    public string getUpdatingGridviewValue(GridView gridviewEntry, string fieldEntry)
        {//start getGridviewValue
            //scan gridview for cell value
                string result = Convert.ToString(functionsOther.getCurrentTime()); 
                for(int i = 0; i < gridviewEntry.HeaderRow.Cells.Count; i++)
                    {//start i for
                        if(gridviewEntry.HeaderRow.Cells[i].Text == fieldEntry)
                            {//start check field match
                                result = gridviewEntry.Rows[rowUpdateIndex].Cells[i].Text;
                                break;
                            }//end check field match
                    }//end i for
            //return
                return result;
        }//end getGridviewValue
    
    0 讨论(0)
  • 2020-12-01 06:36

    You can use the DataRowView to get the column index.

        void OnRequestsGridRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var data = e.Row.DataItem as DataRowView;
    
                // replace request name with a link
                if (data.DataView.Table.Columns["Request Name"] != null)
                {
                    // get the request name
                    string title = data["Request Name"].ToString();
                    // get the column index
                    int idx = data.Row.Table.Columns["Request Name"].Ordinal;
    
                    // ...
    
                    e.Row.Cells[idx].Controls.Clear();
                    e.Row.Cells[idx].Controls.Add(link);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-01 06:38

    A little bug with indexcolumn in alexander's answer: We need to take care of "not found" column:

    int GetColumnIndexByName(GridViewRow row, string columnName)
    {
        int columnIndex = 0;
        int foundIndex=-1;
        foreach (DataControlFieldCell cell in row.Cells)
        {
            if (cell.ContainingField is BoundField)
            {
                if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
                {
                    foundIndex=columnIndex;
                    break;
                }
            }
            columnIndex++; // keep adding 1 while we don't have the correct name
        }
        return foundIndex;
    }
    

    and

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = GetColumnIndexByName(e.Row, "myDataField");
            if( index>0)
            {
                string columnValue = e.Row.Cells[index].Text;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题