By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Get DataGrid Cell Content After Intial Sort

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
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

Nov 16 '05 #2

P: n/a
//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(contentsDataG ridID);

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

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
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


Nov 16 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
//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(contentsDataG ridID);

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

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
>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
>


Nov 16 '05 #4

P: n/a
//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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
//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(contentsDataG ridID);

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

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Steve B." <St****@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
>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
>


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.