473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Changing datagridview cell borders at runtime

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
10 45102
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stan Brown | last post by:
URL: http://www.acad.sunytccc.edu/instruct/sbrown/stat/mygrades.htm CSS: http://www.acad.sunytccc.edu/instruct/sbrown/screen.css Both validate without errors or warnings under W3C validators. ...
5
by: Analysis&Solutions | last post by:
Hi Folks: I've got a weird phenomenon going on with Internet Explorer on the Macintosh... The vertical cell borders are missing between the cells but appear above and below the cells! Here's...
11
by: dgk | last post by:
Is there a way to change the foreground or background color of a single cell in an unbound datagridview?
3
by: Rich | last post by:
Hello, If I want to update data displayed in a datagrideview/datagridview cell, how can I determine what cell I am updating? I am looking at the click event below, for example. Can I get...
7
by: steve | last post by:
Hi All I urgently need help on setting datagridview cell borders at runtime I found some code on the web from Programming Smart Client Data Applications with .NET 2.0 by Brian Noyes See...
17
by: Grant Kelly | last post by:
I'm wondering if it's possible within HTML markup (or possibly CSS) to specify that an HTML table's headers should be placed 'over' the cell borders rather than 'within' the cells. For example...
0
by: hydro123 | last post by:
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following: // initialize varaibles from...
0
by: ziketo | last post by:
Hi, I searched a lot about changing the DataGridView cell borders. BYTES helped me so I will write down the solution: 1. You should override the class DataGridViewTextBoxCell, and the new class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.