如何获得GridView选定的列索引

本文关键字:索引 何获得 GridView | 更新日期: 2023-09-27 18:12:40

我试图从wpf程序中获得Gridview选定的列索引。我可以得到选定的行索引但不能得到选定的列索引

如何获得GridView选定的列索引

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

dataGrid.CurrentColumn.DisplayIndex

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

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

Debasis

这是获取特定单元格值的方法

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

我希望它会帮助你....

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