如何获取GridView选择的列索引

时间:2011-10-27 19:40:38

标签: c# wpf gridview

我正在尝试从wpf程序中获取Gridview选择列索引。我可以获得选定的行索引,但不能获得选定的列索引

3 个答案:

答案 0 :(得分:0)

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

  

dataGrid.CurrentColumn.DisplayIndex

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

希望这些信息对您有所帮助..

此致

Debasis

答案 1 :(得分:0)

这就是我们如何获得特定细胞的价值

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

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

答案 2 :(得分:0)

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