PC Review


Reply
Thread Tools Rate Thread

DataGrid Sorting returns wrong values from dataTable

 
 
vijai thoppae
Guest
Posts: n/a
 
      11th May 2004
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?VHJhdmlzIE1lcmtlbA==?=
Guest
Posts: n/a
 
      12th May 2004
Check out the CurrencyManager class: http://msdn.microsoft.com/library/de...rclasstopic.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.
 
Reply With Quote
 
vijai thoppae
Guest
Posts: n/a
 
      12th May 2004
Thanks for the pointer. Its working now .

~Vijai
"Travis Merkel" <v-(E-Mail Removed)> wrote in message
news:32602D52-607F-43ED-AEF9-(E-Mail Removed)...
> Check out the CurrencyManager class:

http://msdn.microsoft.com/library/de...classtopic.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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datagrid dataset dataview datatable sorting Nathan Franklin Microsoft VB .NET 0 19th Jan 2006 05:15 AM
Sorting DataGrid via SqlCeResultSet verses DataTable? Jen Microsoft Dot NET Compact Framework 3 3rd Oct 2005 10:42 PM
DataGrid bound to DataTable containing blanks returns &nbsp; John A Grandy Microsoft ASP .NET 2 21st Nov 2004 08:34 AM
Sorting datagrid-dataview-datatable =?Utf-8?B?c3ViVA==?= Microsoft ADO .NET 2 27th May 2004 03:56 PM
Datagrid (Winform) / Datatable / Sorting problem Daniel Microsoft ADO .NET 0 30th Nov 2003 05:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:08 AM.