How to get cell value in GridView (WITHOUT using cell index)

心已入冬 提交于 2019-12-30 11:17:07

问题


how to get cell value from gridview without using cell index? Let say the first column name in my table is "RowNumber".

instead of using

string name = GridView1.Rows[0].Cells[0].Text;

Something like

string name = GridView1.Rows[0].Cells["RowNumber"].Text;

回答1:


You could cast the GridViewRow's DataItem property into a DataRowView, and then reference the column names:

DataRowView rowView = (DataRowView)GridView1.Rows[0].DataItem;
string name = rowView["RowNumber"].ToString();

You can't do this from the Cells collection, because they are just TableCell objects, and they don't know anything about the underlying data.

The DataItem property represents the values in that row from the underlying datasource, so that's what you want to deal with.




回答2:


You can use datakeys to access any data you want from the row index.

In the markup of the gridview, add all the fields you want to be able to access to the gridview.

<asp:GridView ID="gvTransactionHistory" runat="server" 
    AutoGenerateColumns="false"  
    onselectedindexchanging="gvTransactionHistory_SelectedIndexChanging" 
    DataKeyNames="ID, AnyField">

These datakeys can be accessed in the code behind with the row index

    var id = gvTransactionHistory.DataKeys[rowIndex].Values["ID"];
    var AnyField = gvTransactionHistory.DataKeys[rowIndex].Values["AnyField"];



回答3:


here is a function I wrote. since we typically get the same list of fields over and over again, I cached the index lookups.

    private static readonly HybridDictionary cache = new HybridDictionary();

    public static object[] GetColumnValues(
        this GridView g, 
        int rownumber, 
        string columnNamesCommaSeparated)
    {
        var dataView = g.DataSource as DataView;
        if (dataView != null)
        {
            DataRow dataRow = dataView[rownumber].Row;
            object[] items = dataRow.ItemArray;
            DataColumnCollection columns = dataRow.Table.Columns;

            string lookupkey = g.ID + columnNamesCommaSeparated;
            var colids = cache[lookupkey] as int[];
            int columnCount;
            if (colids == null)
            {
                string[] columnNames = columnNamesCommaSeparated.Split(',');
                columnCount = columnNames.Count();
                colids = new int[columnCount];

                for (int i = 0; i < columnCount; i++)
                {
                    colids[i] = columns.IndexOf(columnNames[i]);
                }

                cache.Add(lookupkey, colids);
            }
            columnCount = colids.Length;
            var values = new object[columnCount];

            for (int i = 0; i < columnCount; i++)
            {
                values[i] = items[colids[i]] ?? "";
            }
            return values;
        }

        return null;
    }

to use it do something like

            object[] values = g.GetColumnValues(e.Row.DataItemIndex, "Firstname,Lastname,CompanyName");
            if (values != null)
            {
                string header = Server.HtmlEncode(values[0] + " " + values[1] + " @ " + values[2]);
            }
            // do whatever you want with this value


来源:https://stackoverflow.com/questions/19496204/how-to-get-cell-value-in-gridview-without-using-cell-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!