Retrieve Selection from GridDataBoundGrid and get DataRowView[]

Hi,

I would need to know the row selection from a GridDataBoundGrid. Non-contiguous row selection are allowed (with shift).

My function written in C# would return
DataRowView[] SelectedRow(GridDataBoundGrid grid)
{
//implementation here
}

I have found about GridDataBoundGrid.Selections.GetSelectedRows()
but I can''t figure out to get a DataRowView[] out of it.
Also read about grid.BindingContext

CurrencyManager cm = (CurrencyManager) grid.BindingContext[grid.DataSource, grid.DataMember];
cm.List but do not know if it will return Selected rows ?


Can you help ?

Thanks,

Daniel

3 Replies

AD Administrator Syncfusion Team August 16, 2006 12:43 AM UTC

Try this code to see if it does what you need.

DataRowView[] GetSelectedDRV()
{
GridDataBoundGrid grid = this.gridDataBoundGrid1;
GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
ArrayList al = new ArrayList();
foreach(GridRangeInfo range in list)
{
for(int i = range.Top; i <= range.Bottom; i++)
{
al.Add(i-1); //subtract 1 since DataView is zero base, but grid is 1 based
}
}
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
DataView dv = cm.List as DataView;
DataRowView[] drvs = new DataRowView[al.Count];
for(int i = 0; i < al.Count; ++i)
{
drvs[i] = dv[(int)al[i]];
}
return drvs;
}


CG Charbel Gereige September 19, 2006 03:15 PM UTC

is it possible to get the index of the selected rows?

like have
int[] SelectedRowsIndex{
get{
int[] selected;

//.... implementation ....

return selected;
}


AD Administrator Syncfusion Team September 20, 2006 04:53 AM UTC

Hi Charbel,

Use the below code snippet to get the selected rows index in a grid.

int[ ] SelectedRowsIndex
{
get{
GridDataBoundGrid grid = this.gridDataBoundGrid1;
GridRangeInfoList list = grid.Selections.GetSelectedRows(true, false);
ArrayList al = new ArrayList();

foreach(GridRangeInfo range in list)
{
for(int i = range.Top; i <= range.Bottom; i++)
{
al.Add(i-1); //subtract 1 since DataView is zero base, but grid is 1 based
}
}
int[ ] selected = new int[al.Count];
al.CopyTo(selected );

return selected ; //It is a zero based index( DataView index), but grid is 1 based.
}
}

Let me know if it does what you need.

Thanks,
Haneef

Loader.
Up arrow icon