Stavros
Stavros

Reputation: 6130

XtraGrid hardcode one of column's values and use Auto-filter

I have a grid that is binded to a dataset that is populated from a stored-procedure. One of the columns of the dataset (InstrumentName) is not taken from the stored-procedure, but is empty.

I want to set the values on each row of this column, to something, depending on a value of another column (Id). I have a list of names and ids in memony, and I want to hardcode set the name from my list.

When I do that in the Initialized() method, I can see in the debugging that the values are set, but then they are not on the grid. Seems like the grid is repainted and my set values are lost..

I have this loop running in Initialized():

for (int i = 0; i < view.DataRowCount; i++)
{
    object Uic = view.GetRowCellValue(i, view.Columns["Uic"]);
    int UicInt = 0;

    if ((Uic != null) && (int.TryParse(Uic.ToString(), out UicInt)))
         view.SetRowCellValue(i, view.Columns["InstrumentName"], GetName(UicInt));
 }

I have also tried to do it (without looping) at DataMonitorGridViewCustomColumnDisplayText() without success..

In the end, I need to be able to use the AutoFilterRow on the new values. Up to now, everything has been possible, but not the AutoFiltering...

Upvotes: 1

Views: 802

Answers (1)

IAbstract
IAbstract

Reputation: 19881

I would recommend filling a collection from the stored procedure. You can then use that collection as the data source for the grid. Create a class with all appropriate properties including the InstrumentName which you will assign values to from your own in-memory collection.

Update
Based on commentary below - I believe you can use DataRow.RowChanged event to trigger when a row is added. Then grab the 'Id' you want, query your in-memory collection.

I would still go back to my original recommendation. Because DataTables are loosely typed, a strongly-typed class would be much more flexible and ensure data integrity. If your DataTable has the columns Foo (datatype: int), Bar (datatype: string), and Baz (datatype: string) then create a class that mimics that structure:

public class FooBarBazEntity{
    public int Foo { get; set; }
    public string Bar { get; set; }
    public string Baz { get; set; }
}

You can fill a collection - IList<T> or IEnumerable<T> - quite easily with LINQ.

Upvotes: 2

Related Questions