DataGridView multiple tables

T

Tony

Hello!

Assume I have a DataGridView with these column headers in this sequence.
Batch Number TimeLeft Moment

I execute an sql select statement that give me these 10 rows se below. Here
each column match the column header in the DataGridView.
So the result in column 1 is Batch.
Result from column 2 is Number
Result from column 3 is TimeLeft
Result from column 4 is Moment.
36 131 241:22 Pack
36 103 141:44 Pick Y
36 109 198:54 Pick H
36 119 123:12 Pick A
36 129 112:54 Pick I
36 139 118:12 Pick M
36 111 141:65 Pick P
36 133 191:22 Pick D
36 115 111:99 Pick N
36 118 116:91 Pick X


Now assume that I want to display all these 10 rows in a DataGridView in the
way
that 36 must be displayed with a + sign and when clicking on the + the
screen is displaying what is shown just below here with 36 shown only once.
Batch Number TimeLeft Moment
131 241:22 Pack
103 141:44 Pick
109 198:54 Pick H
119 123:12 Pick A
36 129 112:54 Pick I
139 118:12 Pick M
111 141:65 Pick P
133 191:22 Pick D
115 111:99 Pick N
118 116:91 Pick X


Can somebody give me some suggestions how this can be done. I just wonder if
I create two DataTable with a DataRelation in between in the DataSet will
this
automatically create a + sign for column Batch.

//Tony
 
V

vanderghast

I have no trivial solution for the + to be clicked (or unclick), may be you
can use a menu or something along that idea for the 'click' effect, but to
display a blanc if the line has the same value than for the previous line,
you can use the CellFormatting event:


private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex <= 0 || e.ColumnIndex== 0) return; // headers
if (IsTheSameAsPrevious(e.RowIndex, e.ColumnIndex))
{
e.Value = String.Empty;
e.FormattingApplied = true;
}
}


where IsTheSameAsPrevious is a function you write and which is returning
true or false accordingly to your needs. Note that it does not 'center'
vertically the value, but rather list it in the first row where the said
value is used (a little bit like data entry could look, on a sheet of paper,
where the goal is to enter that data). It is doable though to center it,
vertically, but requires extra logic: count the number of time the value is
consecutively found, integer divide by two, add to its first position,
compare that result to the actual e.RowIndex, if equal, display it, if not,
display an empty string.




To remove the thin line between the cells, you can use the CellPainting
event:

private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{

if (e.RowIndex < 0 || e.ColumnIndex < 1) return;

if (IsTheSameAsPrevious(e.RowIndex, e.ColumnIndex))
{
e.AdvancedBorderStyle.Top =
DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top =
DataGridViewAdvancedCellBorderStyle.Single;
}

if (IsTheSameAsNext(e.RowIndex, e.ColumnIndex))
{
e.AdvancedBorderStyle.Bottom =
DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Bottom =
DataGridViewAdvancedCellBorderStyle.Single;
}
}



Where I also use an IsTheSameAsNext function (probably a simple wrapper
making a call to IsTheSameAsPrevious, but changing the argument row value).


If data can be edited, exta logic has to be added to care about values being
modified, breaking the sequence, or increasing it.



Vanderghast, Access MVP
 
T

Tony Johansson

Hello!

I mean if a create two DataTable with DataRows and add these two DataTables
to a DataSet and create a DataRelation because we have relation one to many.
Lets call the DataTables ParentDataTable and ChildDataTable.

Then I assign the the MyDataGridView.DataSource = MyDataSet
Will this create a + sign in the ParentSide so if I click this I can see all
the children.

//Tony
 
J

jp2msft

Common practice for this type of thing is to put your grouped items (i.e.
Batch) in a TreeView control.

This would simplify your life, and you could display or hide the DGV column
with the Batch.
 

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