Hello,

I have a datagrid in my C# application. The Grid has 4 columns, one is a
Boolcoluumn, other 3 are string columns. Out of which bool column is
editable & the last column is editable too. Using DataGridColumnStyle i've
allowed Sorting to true for the whole Datatable. At a time the user is
allowed to Enable only one bool column for the whole Grid. I've handled this
using CurrentCellChanged event for that column by disabling all the row's
bool value & enabling the selected one.Whenever the user clicks the column
header to enable sorting either Ascending or Descending, it sorts
accordingly based upon the clicked column header.
The real problem is after sorting, when the user selects a particular bool
column, in the CurrentCellChanged Event it gives the correct Row Number
according to the selection, but using the Rownumber if i enable that
selected row, it selects a different one rather than the currently selected
row. The selected row happens to be the one which i've constructed in the
begining. After sorting it hasn't updated the DataTable. I tried calling
AcceptChanges() to the DataTable before selecting doesn't seem to work. I'm
not sure is this a bug or a limitation with respect to DataGrid using
DataTable & Dataset. Here's my sample code.


private void CreateDataGrid()
{
myDataSet = new DataSet("myDataSet");
myDataTable = new DataTable("myDataTable");

DataColumn
Parameter_cEnable,Parameter_cDefaultName,Parameter_cParameterInst,Parameter_
cParameterAssign;

Parameter_cEnable = new DataColumn("Select", typeof(bool));

Parameter_cDefaultName = new DataColumn("DefaultName",typeof(string));

Parameter_cParameterInst = new DataColumn("Parameter",typeof(string));

Parameter_cParameterAssign = new
DataColumn("AssignedName",typeof(string));

myDataTable.Columns.Add(Parameter_cEnable);

myDataTable.Columns.Add(Parameter_cDefaultName);

myDataTable.Columns.Add(Parameter_cParameterInst);

myDataTable.Columns.Add(Parameter_cParameterAssign);

myDataSet.Tables.Add(myDataTable);



DataGridTableStyle myDataGridTableStyle = new DataGridTableStyle();

myDataGridTableStyle.MappingName = "myDataTable";

myDataGridTableStyle.RowHeadersVisible = false;

myDataGridTableStyle.ColumnHeadersVisible = false;

myDataGridTableStyle.AllowSorting = true;

myDataGrid.SetDataBinding(myDataSet,"myDataTable");

}

PPLCols.DG_Selection is the bool column

private void __ParmVar_DT_CurrentCellChanged(object sender, System.EventArgs
e)

{

DataGrid dgLocal = (DataGrid)sender;

if (dgLocal.CurrentCell.ColumnNumber == (int)PPLCols.DG_Selection)

{

foreach(DataRow myRow in this.__ParmVar_DataTable.Rows)

{

myRow[(int)PPLCols.DG_Selection] = false;

}

myDataTable.Rows[dgLocal.CurrentRowIndex][(int)PPLCols.DG_Selection]
= true;

__CurrentRowIndex = dgLocal.CurrentRowIndex;

}

}



Any help will be appreciated.

Thanks,

Vijai

RE: DataGrid Sorting returns wrong values from dataTable by v-trmerk

v-trmerk
Wed May 12 14:46:04 CDT 2004

Check out the CurrencyManager class: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscurrencymanagerclasstopic.as

Bind like this

CurrencyManager myCurrencyManager = (CurrencyManager)this.myGrid.BindingContext[myGrid.DataSource]

DataRowView drv = myCurrencyManager.Current as DataRowView

if (drv.Row == null) return

drv.Row["BooleanColumn"] = true

Hope it helps.

Re: DataGrid Sorting returns wrong values from dataTable by vijai

vijai
Wed May 12 15:14:58 CDT 2004

Thanks for the pointer. Its working now :).

~Vijai
"Travis Merkel" <v-trmerk@microsoft.com> wrote in message
news:32602D52-607F-43ED-AEF9-C8179C468E52@microsoft.com...
> Check out the CurrencyManager class:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscurrencymanagerclasstopic.asp
>
> Bind like this:
>
> CurrencyManager myCurrencyManager =
(CurrencyManager)this.myGrid.BindingContext[myGrid.DataSource];
>
> DataRowView drv = myCurrencyManager.Current as DataRowView;
>
> if (drv.Row == null) return;
>
> drv.Row["BooleanColumn"] = true;
>
> Hope it helps.