繁体   English   中英

如何获取GridView选择的列索引

[英]How to get the GridView selected Column index

我试图从WPF程序中获取Gridview选定的列索引。 我可以获取所选的行索引,但不能获取所选的列索引

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {  
    string lbl_nam = (Label)GridView1.Rows[GridView1.SelectedIndex].FindControl("Label_nam");
    string nam = lbl_nam.Text;
  }

如果您可以将GridView控件更改为DataGrid ,则可以尝试下面给出的代码,以从后面的代码获取DataGrid当前列的显示索引:

dataGrid.CurrentColumn.DisplayIndex

DataGrid的CurrentColumn.DisplayIndex属性基本上获取或设置列相对于当前显示的列的显示顺序。 它提供了列在相关联的DataGridView中显示的从零开始的位置,或者如果控件中不包含该范围,则返回-1。

希望这些信息对您有帮助。

问候

德巴斯

这就是我们如何获取特定单元格的值

Object obj = GetCell(3).Content;
                     string cellContent = String.Empty;
                     if (obj != null)
                     {
                         if (obj is TextBox)
                             cellContent = ((TextBox)(obj)).Text;
                         else
                             cellContent = ((TextBlock)(obj)).Text;
                      }




private DataGridCell GetCell(int column)
    {
        DataGridRow rowContainer = GetRow();

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // Try to get the cell but it may possibly be virtualized.
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // Now try to bring into view and retreive the cell.
                customDataGrid.UCdataGridView.ScrollIntoView(rowContainer, customDataGrid.UCdataGridView.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }

我希望它将对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM