Get DataGrid Cell Content After Intial Sort

G

Guest

I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's
reflect the contents of the grid cells.

Sorting of columns (both thru VS and programmatically) work fine except,
when the form/grid first opens up and the grid is immediately sorted the TB
don't reflect the sorted data of the First row of the grid.

Note: Initially the black grid indicator arrow points to the first row. If
the user choses another row and then sorts the TB's reflect proper data.

//setGridSort(DataSet dataSet, DataGrid dataGrid, string
sortTableColumnName, int tableNo)
DataView dataView = createNewDataView();
dataView = dataSet.Tables[tableNo].DefaultView;
dataView.Sort = sortTableColumnName;
dataGrid.DataSource = dataView;

//incorrect - not the correct cell value of the grid for the TB
numberTB.Text = (string) (dataGrid[dataGrid.CurrentRowIndex, columnNo]);

Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

Are you setting up the data binding correctly? Is the data source that
you are connecting the grid to the same as the data source that you are
binding the textboxes to?

Can you show a piece of code that shows the issue?
 
G

Guest

//I hope this is the info your looking for
//The NumberTB property VS window under Data->DataBinding->Text says
configMgmtLogDS - StevesConfigMgmtLog.DocumentNumber
//and nothing else (Tag, etc)

//The InitializeComponent() says
this.dGrid.DataSource = this.configMgmtLogDS.StevesConfigMgmtLog;
this.numberTB.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.configMgmtLogDS, "StevesConfigMgmtLog.DocumentNumber"));

ConfigMgmtLog_Load() //ConfigMgmtLog is Access dB
{
//opens the database and fills the DS
LoadDataFromDataBase();

//load the DataTable for the dbaseTableName, 1 per class
LoadDataTable();

//sets the sort and binds the DataView to the DataGrid
setGridSort(configMgmtLogDS, dGrid, gridSortedByColumn, numberZero);

setGridTableStyle(dGrid, configMgmtLogDS);

//change column names and width
setGridNamesAndWidth(dGrid);

bindDataBase_To_DataGrid(); //binding at startup (no event)

OrganizeDataGridColums(dGrid); //
}

bindDataBase_To_DataGrid()
{
int idColumnNo = getDataSetColumnNumber(configMgmtLogDS, dbaseTableName,
"ID");

if (dGrid.CurrentRowIndex >= 0)
{
int contentsDataGridID = (int) (dGrid[dGrid.CurrentRowIndex, idColumnNo]);
int matchingDataSetRowNumber =
findMatchingDataGridDataSetRowNumber(contentsDataGridID);

if (matchingDataSetRowNumber >= 0)
this.BindingContext[configMgmtLogDS, dbaseTableName].Position =
matchingDataSetRowNumber;
}
}



Nicholas Paldino said:
Steve,

Are you setting up the data binding correctly? Is the data source that
you are connecting the grid to the same as the data source that you are
binding the textboxes to?

Can you show a piece of code that shows the issue?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve B. said:
I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's
reflect the contents of the grid cells.

Sorting of columns (both thru VS and programmatically) work fine except,
when the form/grid first opens up and the grid is immediately sorted the
TB
don't reflect the sorted data of the First row of the grid.

Note: Initially the black grid indicator arrow points to the first row. If
the user choses another row and then sorts the TB's reflect proper data.

//setGridSort(DataSet dataSet, DataGrid dataGrid, string
sortTableColumnName, int tableNo)
DataView dataView = createNewDataView();
dataView = dataSet.Tables[tableNo].DefaultView;
dataView.Sort = sortTableColumnName;
dataGrid.DataSource = dataView;

//incorrect - not the correct cell value of the grid for the TB
numberTB.Text = (string) (dataGrid[dataGrid.CurrentRowIndex, columnNo]);

Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

It looks like you are using two separate data sources. The grid is
binding to this.configMgmtLogDS.StevesConfigMgmtLog, while the textbox is
binding to this.configMgmtLogDS. Even though the binding says to get member
"StevesConfigMgmtLog.DocumentNumber", which effectively gets the data table
StevesConfigMgmtLog, they are two separate data sources.

You should either change the grid data source to be this.configMgmtLogDS
and set the data member to "StevesConfigMgmtLog", or switch the data source
of the TextBox binding to this.configMgmtLogDS.StevesConfigMgmtLog, and set
the member to DocumentNumber.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve B. said:
//I hope this is the info your looking for
//The NumberTB property VS window under Data->DataBinding->Text says
configMgmtLogDS - StevesConfigMgmtLog.DocumentNumber
//and nothing else (Tag, etc)

//The InitializeComponent() says
this.dGrid.DataSource = this.configMgmtLogDS.StevesConfigMgmtLog;
this.numberTB.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.configMgmtLogDS, "StevesConfigMgmtLog.DocumentNumber"));

ConfigMgmtLog_Load() //ConfigMgmtLog is Access dB
{
//opens the database and fills the DS
LoadDataFromDataBase();

//load the DataTable for the dbaseTableName, 1 per class
LoadDataTable();

//sets the sort and binds the DataView to the DataGrid
setGridSort(configMgmtLogDS, dGrid, gridSortedByColumn, numberZero);

setGridTableStyle(dGrid, configMgmtLogDS);

//change column names and width
setGridNamesAndWidth(dGrid);

bindDataBase_To_DataGrid(); //binding at startup (no event)

OrganizeDataGridColums(dGrid); //
}

bindDataBase_To_DataGrid()
{
int idColumnNo = getDataSetColumnNumber(configMgmtLogDS, dbaseTableName,
"ID");

if (dGrid.CurrentRowIndex >= 0)
{
int contentsDataGridID = (int) (dGrid[dGrid.CurrentRowIndex,
idColumnNo]);
int matchingDataSetRowNumber =
findMatchingDataGridDataSetRowNumber(contentsDataGridID);

if (matchingDataSetRowNumber >= 0)
this.BindingContext[configMgmtLogDS, dbaseTableName].Position =
matchingDataSetRowNumber;
}
}



Nicholas Paldino said:
Steve,

Are you setting up the data binding correctly? Is the data source
that
you are connecting the grid to the same as the data source that you are
binding the textboxes to?

Can you show a piece of code that shows the issue?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve B. said:
I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's
reflect the contents of the grid cells.

Sorting of columns (both thru VS and programmatically) work fine
except,
when the form/grid first opens up and the grid is immediately sorted
the
TB
don't reflect the sorted data of the First row of the grid.

Note: Initially the black grid indicator arrow points to the first row.
If
the user choses another row and then sorts the TB's reflect proper
data.

//setGridSort(DataSet dataSet, DataGrid dataGrid, string
sortTableColumnName, int tableNo)
DataView dataView = createNewDataView();
dataView = dataSet.Tables[tableNo].DefaultView;
dataView.Sort = sortTableColumnName;
dataGrid.DataSource = dataView;

//incorrect - not the correct cell value of the grid for the TB
numberTB.Text = (string) (dataGrid[dataGrid.CurrentRowIndex,
columnNo]);

Steve
 
G

Guest

//I really wish I had success BUT, currently:

//See last thread entry for method content
setGridSort(DataSet dataSet, DataGrid dataGrid,
string sortTableColumnName, int tableNo);

//current dGrid Bindings
this.dGrid.DataMember = "StevesConfigMgmtLog";
this.dGrid.DataSource = this.configMgmtLogDS;

//current TextBox Binding
this.numberTB.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.configMgmtLogDS, "StevesConfigMgmtLog.DocumentNumber"));

//AFTER GRID SORT
//and not moving the black grid indicator arrowhead

//incorrent - displays first entry in the DataSet
numberTB.Text = (string) configMgmtLogDS.Tables[dbaseTableName].Rows[0][5];

//incorrent - displays entry that shown in grid before grid sort
numberTB.Text = (string) dGrid[0, 5];

Steve

Nicholas Paldino said:
Steve,

It looks like you are using two separate data sources. The grid is
binding to this.configMgmtLogDS.StevesConfigMgmtLog, while the textbox is
binding to this.configMgmtLogDS. Even though the binding says to get member
"StevesConfigMgmtLog.DocumentNumber", which effectively gets the data table
StevesConfigMgmtLog, they are two separate data sources.

You should either change the grid data source to be this.configMgmtLogDS
and set the data member to "StevesConfigMgmtLog", or switch the data source
of the TextBox binding to this.configMgmtLogDS.StevesConfigMgmtLog, and set
the member to DocumentNumber.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve B. said:
//I hope this is the info your looking for
//The NumberTB property VS window under Data->DataBinding->Text says
configMgmtLogDS - StevesConfigMgmtLog.DocumentNumber
//and nothing else (Tag, etc)

//The InitializeComponent() says
this.dGrid.DataSource = this.configMgmtLogDS.StevesConfigMgmtLog;
this.numberTB.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.configMgmtLogDS, "StevesConfigMgmtLog.DocumentNumber"));

ConfigMgmtLog_Load() //ConfigMgmtLog is Access dB
{
//opens the database and fills the DS
LoadDataFromDataBase();

//load the DataTable for the dbaseTableName, 1 per class
LoadDataTable();

//sets the sort and binds the DataView to the DataGrid
setGridSort(configMgmtLogDS, dGrid, gridSortedByColumn, numberZero);

setGridTableStyle(dGrid, configMgmtLogDS);

//change column names and width
setGridNamesAndWidth(dGrid);

bindDataBase_To_DataGrid(); //binding at startup (no event)

OrganizeDataGridColums(dGrid); //
}

bindDataBase_To_DataGrid()
{
int idColumnNo = getDataSetColumnNumber(configMgmtLogDS, dbaseTableName,
"ID");

if (dGrid.CurrentRowIndex >= 0)
{
int contentsDataGridID = (int) (dGrid[dGrid.CurrentRowIndex,
idColumnNo]);
int matchingDataSetRowNumber =
findMatchingDataGridDataSetRowNumber(contentsDataGridID);

if (matchingDataSetRowNumber >= 0)
this.BindingContext[configMgmtLogDS, dbaseTableName].Position =
matchingDataSetRowNumber;
}
}



Nicholas Paldino said:
Steve,

Are you setting up the data binding correctly? Is the data source
that
you are connecting the grid to the same as the data source that you are
binding the textboxes to?

Can you show a piece of code that shows the issue?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's
reflect the contents of the grid cells.

Sorting of columns (both thru VS and programmatically) work fine
except,
when the form/grid first opens up and the grid is immediately sorted
the
TB
don't reflect the sorted data of the First row of the grid.

Note: Initially the black grid indicator arrow points to the first row.
If
the user choses another row and then sorts the TB's reflect proper
data.

//setGridSort(DataSet dataSet, DataGrid dataGrid, string
sortTableColumnName, int tableNo)
DataView dataView = createNewDataView();
dataView = dataSet.Tables[tableNo].DefaultView;
dataView.Sort = sortTableColumnName;
dataGrid.DataSource = dataView;

//incorrect - not the correct cell value of the grid for the TB
numberTB.Text = (string) (dataGrid[dataGrid.CurrentRowIndex,
columnNo]);

Steve
 

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