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

Changing datagridview cell borders at runtime

P: n/a
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.T oString <> "" Then

Dim mystyle As New DataGridViewAdvancedBorderStyle

mystyle.Top = DataGridViewAdvancedCellBorderStyle.None

Dim myplaceholder As New DataGridViewAdvancedBorderStyle

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

End If
Jun 13 '06 #1
Share this Question
Share on Google+
10 Replies


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

"steve" <ga*****@newsgroups.nospam> schreef in bericht
news:us**************@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.T oString <> "" Then

Dim mystyle As New DataGridViewAdvancedBorderStyle

mystyle.Top = DataGridViewAdvancedCellBorderStyle.None

Dim myplaceholder As New DataGridViewAdvancedBorderStyle

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

End If

Jun 14 '06 #2

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

Jun 14 '06 #3

P: n/a
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\WindowsApp lication1\Form1.vb 1 14
WindowsApplication1
Error 2 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
2 1 WindowsApplication1
Error 3 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 3 5
WindowsApplication1
Error 4 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 4 5
WindowsApplication1
Error 5 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 5 5
WindowsApplication1
Error 6 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\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\WindowsApp lication1\Form1.vb 8 5
WindowsApplication1
Error 8 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
9 5 WindowsApplication1
Error 9 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 10 9
WindowsApplication1
Error 10 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 11 9
WindowsApplication1
Error 11 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 12 9
WindowsApplication1
Error 12 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 13 9
WindowsApplication1
Error 13 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 14 9
WindowsApplication1
Error 14 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
15 5 WindowsApplication1
Error 15 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 16 5
WindowsApplication1
Error 16 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
18 1 WindowsApplication1
Error 17 End of statement expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 20 14
WindowsApplication1
Error 18 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
21 1 WindowsApplication1
Error 19 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 22 5
WindowsApplication1
Error 20 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 23 1
WindowsApplication1
Error 21 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 24 5
WindowsApplication1
Error 22 Declaration expected. C:\Documents and Settings\cor\Mijn
documenten\Visual Studio
2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb 25 1
WindowsApplication1
Error 23 Syntax error. C:\Documents and Settings\cor\Mijn documenten\Visual
Studio 2005\Projects\Tests\WindowsApplication1\WindowsApp lication1\Form1.vb
26 1 WindowsApplication1

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> schreef in bericht
news:NC**************@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.

Jun 14 '06 #4

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

Jun 14 '06 #5

P: n/a
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
news:NC**************@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.

Jun 14 '06 #6

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

Jun 16 '06 #7

P: n/a
What I know is OrchidGrid at http://www.springsys.com supports cell merging
similar to excel.

"steve" <ga*****@newsgroups.nospam> us**************@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.T oString <> "" Then

Dim mystyle As New DataGridViewAdvancedBorderStyle

mystyle.Top = DataGridViewAdvancedCellBorderStyle.None

Dim myplaceholder As New DataGridViewAdvancedBorderStyle

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

End If

Jun 17 '06 #8

P: n/a
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
news:L7**************@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.

Jun 17 '06 #9

P: n/a
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.AdvancedCellBord erStyle,
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.

Jun 19 '06 #10

P: n/a
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
news:90*************@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.AdvancedCellBord erStyle,
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.

Jun 19 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.