在asp.net中如何通过列名获取单元格值而不是通过GridView中的索引

我在asp.net中有一个gridview ,现在我想通过列名称,而不是单元格索引的单元格值。

如何通过单元格列名检索单元格值是可能的

GridView不作为列名称,因为它是datasource属性来知道这些事情。

如果您仍然需要知道给定列名的索引,那么您可以创build一个帮助器方法来执行此操作,因为gridview Header通常包含此信息。

 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; } 

请记住,上面的代码将使用BoundField …然后使用它:

 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; } } 

我强烈build议您使用TemplateField来拥有自己的控件,那么抓住这些控件就更容易了:

 <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> 

然后使用

 string columnValue = ((Label)e.Row.FindControl("lblName")).Text; 

虽然它很长一段时间,但是这个相对较小的代码似乎很容易阅读和得到:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { int index; string cellContent; foreach (TableCell tc in ((GridView)sender).HeaderRow.Cells) { if( tc.Text.Equals("yourColumnName") ) { index = ((GridView)sender).HeaderRow.Cells.GetCellIndex(tc); cellContent = ((GridView)sender).SelectedRow.Cells[index].Text; break; } } } 

您可以使用DataRowView来获取列索引。

  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); } } } 

对于Lambda爱好者

 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var boundFields = e.Row.Cells.Cast<DataControlFieldCell>() .Select(cell => cell.ContainingField).Cast<BoundField>().ToList(); int idx = boundFields.IndexOf( boundFields.FirstOrDefault(f => f.DataField == "ColName")); e.Row.Cells[idx].Text = modification; } } 

在亚历山大的答案索引列一个小bug:我们需要照顾“找不到”列:

 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; } 

 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; } } }