1

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).

I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.

Valerij
  • 27,090
  • 1
  • 26
  • 42
prabhakar
  • 57
  • 1
  • 3
  • 12

2 Answers2

0

The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.

Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.

// Get the Row Count
int rowCount = myGrid.Items.Count;

// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
Arul Dinesh
  • 550
  • 4
  • 15
  • hi your code int colCount = myGrid.Items[0].Cells.Count; not working there is error at .cells.my real question is for ex if i click the first column in the datagrid i want the column count as 0 and so on how could i do it in selection change event – prabhakar Oct 24 '13 at 06:29
  • Do you mean the selected index? int selectedIndex = myGrid.SelectedIndex; – Arul Dinesh Oct 24 '13 at 06:35
  • no i am having column monday to friday in datagrid if i click the monday column i want that column count or column header name .even if i can able to get the clicked column header name it will be ok for me – prabhakar Oct 24 '13 at 07:12
  • Items refers to Rows, not columns. – ouflak Oct 24 '13 at 08:00
  • So I think what you want is the Index of the current column selected. – ouflak Oct 24 '13 at 08:15
  • 1
    try this one :-int index = dataGrid1.SelectedCells[0].Column.DisplayIndex; object header = dataGrid1.Columns[index].Header; – Arul Dinesh Oct 24 '13 at 08:20
  • Arul, that's along the right lines, but what if no columns are selected, or multiple columns are selected? I think my answer covers those scenarios. – ouflak Oct 24 '13 at 09:01
  • 1
    hi thanks for your help i already try this one in the selection change event it will work for me i hope it will be helpful for others DataGrid x = (DataGrid)this.FindName("attendancegrid"); var index = x.CurrentColumn; if (index == null) { return; } else { columnname = x.CurrentColumn.Header.ToString(); } – prabhakar Oct 24 '13 at 10:20
  • That should work just fine if there is only ever going to be just one or column selected or none selected as opposed to several. Or if you are only interested in the very last column selected out of a group. – ouflak Oct 24 '13 at 10:40
0

I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:

    List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);

    for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
    {
        foreach (DataGridColumn column in dataGrid.Columns)
        {
            if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex) 
            {
                    if (!selectedColumnIndexes.Contains(column.DisplayIndex))
                    {
                        selectedColumnIndexes.Add(column.DisplayIndex);
                    }
            }
        }
    }

Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.

Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

Community
  • 1
  • 1
ouflak
  • 2,458
  • 10
  • 44
  • 49