Home All Groups Group Topic Archive Search About

Changing datagridview cell borders at runtime

Author
13 Jun 2006 11:06 PM
steve
Hi All

I would like to be able to change the cell borders on certain cells to none
at runtime to make a group of cells appear to be merged

I have tried the following in the cellformatting event..., to no avail

Any ideas

Regards
Steve
If dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString <> "" Then

Dim mystyle As New DataGridViewAdvancedBorderStyle

mystyle.Top = DataGridViewAdvancedCellBorderStyle.None

Dim myplaceholder As New DataGridViewAdvancedBorderStyle

dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).AdjustCellBorderStyle(mystyle,
myplaceholder, False, False, False, False)

End If

Author
14 Jun 2006 6:22 AM
Cor Ligthert [MVP]
steve,

I never have seen that this was possible. The solution for this is to create
extra columns in your datatable with an expression which concatenate two
columns.

http://www.vb-tips.com/default.aspx?ID=e8fee3e3-3ca7-4bb0-81dc-f0b560f43e7c

That it is about a combobox is of course only a detail the solution is the
same for any complex datacontrol.

I hope this helps,

Cor

Show quoteHide quote
"steve" <ga630sf@newsgroups.nospam> schreef in bericht
news:us8g23zjGHA.4748@TK2MSFTNGP04.phx.gbl...
> Hi All
>
> I would like to be able to change the cell borders on certain cells to
> none at runtime to make a group of cells appear to be merged
>
> I have tried the following in the cellformatting event..., to no avail
>
> Any ideas
>
> Regards
> Steve
> If dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString <> "" Then
>
> Dim mystyle As New DataGridViewAdvancedBorderStyle
>
> mystyle.Top = DataGridViewAdvancedCellBorderStyle.None
>
> Dim myplaceholder As New DataGridViewAdvancedBorderStyle
>
> dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).AdjustCellBorderStyle(mystyle,
> myplaceholder, False, False, False, False)
>
> End If
>
>
Author
14 Jun 2006 7:19 AM
Jeffrey Tan[MSFT]
Hi Steve,

Thanks for your post!

I have tried to use the code snippet below to set all the cells' left and
right border to none:
private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("column1", typeof(int));
    dt.Columns.Add("column2", typeof(string));
    dt.Columns.Add("column3", typeof(string));

    for (int i = 0; i < 20; i++)
    {
        DataRow dr = dt.NewRow();
        dr["column1"] = i;
        dr["column2"] = "item"+i.ToString ();
        dr["column3"] = "column3";
        dt.Rows.Add(dr);
    }
    this.dataGridView1.DataSource = dt;

}

private void button1_Click(object sender, EventArgs e)
{
    this.dataGridView1.AdvancedCellBorderStyle.Left =
DataGridViewAdvancedCellBorderStyle.None;
    this.dataGridView1.AdvancedCellBorderStyle.Right =
DataGridViewAdvancedCellBorderStyle.None;
}

This works well on my side. It has the effect of merging all the cells in a
row into a single cell. Does it meet your need?

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
14 Jun 2006 7:43 AM
Cor Ligthert [MVP]
Jeffrey,

Thank you showing this. Feel free to tell me this direct, it shows me that I
have to take more time in the datagridview. It is not I did that already,
but I see that it is not enough, I base my answers still to much about the
datagrid.

Do you mind,  if I set your (a little bit changed and in VB) sample on our
website. Please tell than if that is OK that I than tell that you made it
(not referencing microsoft) just as newsgroup contributer or without that.

I would like it with your name otherwise it looks if we did make that.

Your sample gave me however the errors below when I did try it in VBNet (I
would use next time the loaddatarow, that is much easier to create a
datatable).

:-)

Cor

Error 1 End of statement expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 1 14
WindowsApplication1
Error 2 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
2 1 WindowsApplication1
Error 3 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 3 5
WindowsApplication1
Error 4 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 4 5
WindowsApplication1
Error 5 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 5 5
WindowsApplication1
Error 6 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 6 5
WindowsApplication1
Error 7 Statement cannot appear outside of a method body. C:\Documents and
Settings\cor\Mijn documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 8 5
WindowsApplication1
Error 8 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
9 5 WindowsApplication1
Error 9 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 10 9
WindowsApplication1
Error 10 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 11 9
WindowsApplication1
Error 11 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 12 9
WindowsApplication1
Error 12 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 13 9
WindowsApplication1
Error 13 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 14 9
WindowsApplication1
Error 14 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
15 5 WindowsApplication1
Error 15 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 16 5
WindowsApplication1
Error 16 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
18 1 WindowsApplication1
Error 17 End of statement expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 20 14
WindowsApplication1
Error 18 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
21 1 WindowsApplication1
Error 19 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 22 5
WindowsApplication1
Error 20 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 23 1
WindowsApplication1
Error 21 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 24 5
WindowsApplication1
Error 22 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb 25 1
WindowsApplication1
Error 23 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApplication1\Form1.vb
26 1 WindowsApplication1



""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> schreef in bericht
Show quoteHide quote
news:NCon4L4jGHA.5832@TK2MSFTNGXA01.phx.gbl...
> Hi Steve,
>
> Thanks for your post!
>
> I have tried to use the code snippet below to set all the cells' left and
> right border to none:
> private void Form1_Load(object sender, EventArgs e)
> {
>    DataTable dt = new DataTable();
>    dt.Columns.Add("column1", typeof(int));
>    dt.Columns.Add("column2", typeof(string));
>    dt.Columns.Add("column3", typeof(string));
>
>    for (int i = 0; i < 20; i++)
>    {
>        DataRow dr = dt.NewRow();
>        dr["column1"] = i;
>        dr["column2"] = "item"+i.ToString ();
>        dr["column3"] = "column3";
>        dt.Rows.Add(dr);
>    }
>    this.dataGridView1.DataSource = dt;
>
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
>    this.dataGridView1.AdvancedCellBorderStyle.Left =
> DataGridViewAdvancedCellBorderStyle.None;
>    this.dataGridView1.AdvancedCellBorderStyle.Right =
> DataGridViewAdvancedCellBorderStyle.None;
> }
>
> This works well on my side. It has the effect of merging all the cells in
> a
> row into a single cell. Does it meet your need?
>
> Hope it helps.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
Author
14 Jun 2006 8:15 AM
Jeffrey Tan[MSFT]
Hi Cor,

Yes, I forget to translate this code from C# to VB.net :-(. Below is the
modified VB.net version:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("column1", GetType(Integer)))
        dt.Columns.Add(New DataColumn("column2", GetType(String)))

        Dim i As Integer
        For i = 0 To 4
            Dim dr As DataRow = dt.NewRow()
            dr("column1") = i
            dr("column2") = "item" & i.ToString()
            dt.Rows.Add(dr)
        Next

        Me.DataGridView1.DataSource = dt
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Me.DataGridView1.AdvancedCellBorderStyle.Left =
DataGridViewAdvancedCellBorderStyle.None
        Me.DataGridView1.AdvancedCellBorderStyle.Right =
DataGridViewAdvancedCellBorderStyle.None
    End Sub
End Class

.Net2.0 DataGridView has got a significant change from DataGrid, below is a
good resource written by our product team. It gives DataGridView control
model a good explanation:
http://www.windowsforms.net/Samples/Go%20To%20Market/DataGridView/DataGridVi
ew%20FAQ.doc

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
14 Jun 2006 10:03 AM
steve
Hi Jeff

Thanks for the prompt reply

I actually need to set the bottom borders to none on only certain cells and
only within 1 column

I have staff members in each column and times of day for each row

e.g
If an appointment spans 2 vertical cells I want to set the border between
them to none so they look like 1 cell

The rest of the cells will have normal borders


Regards
Steve


""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> wrote in message
Show quoteHide quote
news:NCon4L4jGHA.5832@TK2MSFTNGXA01.phx.gbl...
> Hi Steve,
>
> Thanks for your post!
>
> I have tried to use the code snippet below to set all the cells' left and
> right border to none:
> private void Form1_Load(object sender, EventArgs e)
> {
>    DataTable dt = new DataTable();
>    dt.Columns.Add("column1", typeof(int));
>    dt.Columns.Add("column2", typeof(string));
>    dt.Columns.Add("column3", typeof(string));
>
>    for (int i = 0; i < 20; i++)
>    {
>        DataRow dr = dt.NewRow();
>        dr["column1"] = i;
>        dr["column2"] = "item"+i.ToString ();
>        dr["column3"] = "column3";
>        dt.Rows.Add(dr);
>    }
>    this.dataGridView1.DataSource = dt;
>
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
>    this.dataGridView1.AdvancedCellBorderStyle.Left =
> DataGridViewAdvancedCellBorderStyle.None;
>    this.dataGridView1.AdvancedCellBorderStyle.Right =
> DataGridViewAdvancedCellBorderStyle.None;
> }
>
> This works well on my side. It has the effect of merging all the cells in
> a
> row into a single cell. Does it meet your need?
>
> Hope it helps.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
Author
16 Jun 2006 5:39 AM
Jeffrey Tan[MSFT]
Hi steve,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
17 Jun 2006 2:35 AM
steve
Hi Jeff

I sent a reply on 14/6 to your code for changing the borders

I actually need to set the bottom border of specific cells to none

This is so it will appear that the cell spans > 1 row

Not set all cells bottom borders to none etc

Hence I need to access borders at the cell level rather than grid level


Regards
Steve

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> wrote in message
Show quoteHide quote
news:L7Y68cQkGHA.5184@TK2MSFTNGXA01.phx.gbl...
> Hi steve,
>
> Does my reply make sense to you? Is your problem resolved? Please feel
> free
> to tell me, thanks!
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
Author
19 Jun 2006 9:37 AM
Jeffrey Tan[MSFT]
Hi steve,

Thanks for your feedback!

Ok, I understand your requirement now.

Currently, the DataGridView control model does not expose an easy interface
for setting individual cell border. DataGridViewCell.AdjustCellBorderStyle
method is not used to modify the cell border publicly. It is used
internally by DataGridView painting engine to get the paiting style in the
painting process. Below is the code snippet I got from Reflector:

protected internal virtual void PaintCells(Graphics graphics, Rectangle
clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates
rowState, bool isFirstDisplayedRow, bool isLastVisibleRow,
DataGridViewPaintParts paintParts)
{
                .......
    style3 = cell1.AdjustCellBorderStyle(view1.AdvancedCellBorderStyle,
style2, view1.SingleVerticalBorderAdded, view1.SingleHorizontalBorderAdded,
flag1, isFirstDisplayedRow);
                cell1.PaintWork(graphics, clipBounds, rectangle1, rowIndex,
states1, style1, style3, paintParts);
                ........
}
As you can see, the DataGridView invokes
DataGridViewCell.AdjustCellBorderStyle to get the modified cell border
style then uses it to paint the cell. So you can inherit from the
DataGridViewCell class and overrides AdjustCellBorderStyle method to get a
customized version of DataGridViewCell. Then you can use this customized
DataGridViewCell in your DataGridView.
Note:
In your customized DataGridViewCell, you should expose a
DataGridViewAdvancedBorderStyle public member so that DataGridView code can
set this member border style information to the cell. Then in the
AdjustCellBorderStyle implementation, you should check the this
DataGridViewAdvancedBorderStyle public member and return corresponding
DataGridViewAdvancedBorderStyle. Then DataGridView PaintCells can use it to
paint your cell.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
19 Jun 2006 10:16 AM
Cor Ligthert [MVP]
Jeffrey,

Steve has replied his message somewhere else, I have given him there the
advice if he does not succeed just to take the old simple DataGrid sollution
as I have showed in this message.

Cor

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> schreef in bericht
Show quoteHide quote
news:90zqSQ4kGHA.864@TK2MSFTNGXA01.phx.gbl...
> Hi steve,
>
> Thanks for your feedback!
>
> Ok, I understand your requirement now.
>
> Currently, the DataGridView control model does not expose an easy
> interface
> for setting individual cell border. DataGridViewCell.AdjustCellBorderStyle
> method is not used to modify the cell border publicly. It is used
> internally by DataGridView painting engine to get the paiting style in the
> painting process. Below is the code snippet I got from Reflector:
>
> protected internal virtual void PaintCells(Graphics graphics, Rectangle
> clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates
> rowState, bool isFirstDisplayedRow, bool isLastVisibleRow,
> DataGridViewPaintParts paintParts)
> {
>                .......
> style3 = cell1.AdjustCellBorderStyle(view1.AdvancedCellBorderStyle,
> style2, view1.SingleVerticalBorderAdded,
> view1.SingleHorizontalBorderAdded,
> flag1, isFirstDisplayedRow);
>                cell1.PaintWork(graphics, clipBounds, rectangle1, rowIndex,
> states1, style1, style3, paintParts);
>                ........
> }
> As you can see, the DataGridView invokes
> DataGridViewCell.AdjustCellBorderStyle to get the modified cell border
> style then uses it to paint the cell. So you can inherit from the
> DataGridViewCell class and overrides AdjustCellBorderStyle method to get a
> customized version of DataGridViewCell. Then you can use this customized
> DataGridViewCell in your DataGridView.
> Note:
> In your customized DataGridViewCell, you should expose a
> DataGridViewAdvancedBorderStyle public member so that DataGridView code
> can
> set this member border style information to the cell. Then in the
> AdjustCellBorderStyle implementation, you should check the this
> DataGridViewAdvancedBorderStyle public member and return corresponding
> DataGridViewAdvancedBorderStyle. Then DataGridView PaintCells can use it
> to
> paint your cell.
>
> Hope this helps.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
Author
17 Jun 2006 1:21 AM
Jeff Glatt
What I know is OrchidGrid at http://www.springsys.com supports cell merging
similar to excel.



Show quoteHide quote
"steve" <ga630sf@newsgroups.nospam> us8g23zjGHA.4***@TK2MSFTNGP04.phx.gbl...
> Hi All
>
> I would like to be able to change the cell borders on certain cells to
> none at runtime to make a group of cells appear to be merged
>
> I have tried the following in the cellformatting event..., to no avail
>
> Any ideas
>
> Regards
> Steve
> If dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString <> "" Then
>
> Dim mystyle As New DataGridViewAdvancedBorderStyle
>
> mystyle.Top = DataGridViewAdvancedCellBorderStyle.None
>
> Dim myplaceholder As New DataGridViewAdvancedBorderStyle
>
> dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).AdjustCellBorderStyle(mystyle,
> myplaceholder, False, False, False, False)
>
> End If
>
>