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