469,602 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Get DataGrid Cell Content After Intial Sort

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
4 2622
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
//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
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
//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.

Similar topics

2 posts views Thread by Chris Plowman | last post: by
1 post views Thread by Sargas Atum | last post: by
2 posts views Thread by Daniel Walzenbach | last post: by
10 posts views Thread by Kejpa | last post: by
2 posts views Thread by Peter | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.