DataGrid Sorting returns wrong values from dataTable

V

vijai thoppae

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top