如何获取GridView选中的Column索引

发布于 2024-12-12 02:00:24 字数 62 浏览 0 评论 0原文

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

i am trying to get the Gridview selected column index from wpf program. i can get the selected row index but not the selected colum index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱&纷飞 2024-12-19 02:00:24

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

dataGrid.CurrentColumn.DisplayIndex

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

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

问候

Debasis

If you are in a position to change GridView control to DataGrid, you can try the code given below to get the current column's display index of the DataGrid from code behind:

dataGrid.CurrentColumn.DisplayIndex

This CurrentColumn.DisplayIndex property of DataGrid basically gets or sets the display order of the column relative to the currently displayed columns. It provides you the zero-based position of the column as it is displayed in the associated DataGridView, or -1 if the band is not contained within a control.

Hope the information will be helpful for you ..

Regards

Debasis

橪书 2024-12-19 02:00:24

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

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

,希望它能对您有所帮助......

This is how we can get the value of a specific cell

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

i hope it will helps you....

我一向站在原地 2024-12-19 02:00:24
 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {  
    string lbl_nam = (Label)GridView1.Rows[GridView1.SelectedIndex].FindControl("Label_nam");
    string nam = lbl_nam.Text;
  }
 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
 {  
    string lbl_nam = (Label)GridView1.Rows[GridView1.SelectedIndex].FindControl("Label_nam");
    string nam = lbl_nam.Text;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文