Computer Guy

I am using this code to color the grid view according to some conditions :

Gridview1.Rows(X).Cells(Y).Style.BackColor = Color.Green

And I set the AllowUserToOrderColumns property to false.

But when I run the program and click on the columns header it orders the columns and remove all back colors . so if any one know how to make the back color property permenent I would be greatfull.

Thank you



Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Bob zhu - MSFT

In winform you can use datagridview columnstyle to define select back color as follow

private void button1_Click(object sender, EventArgs e)

{

DataTable tb = new MyGenTable().get_TBA();

this.dataGridView1.AutoGenerateColumns = false;

this.dataGridView1.DataSource = tb;

DataGridViewTextBoxColumn tc1 = new DataGridViewTextBoxColumn();

tc1.DataPropertyName = "ID";

this.dataGridView1.Columns.Add(tc1);

DataGridViewCellStyle style=new DataGridViewCellStyle ();

style.SelectionBackColor=Color.Red;

this.dataGridView1.Columns[0].DefaultCellStyle = style;

}

But if you mention is asp.net GridView control, you can post it to

http://forums.asp.net

for more info.

Best regards!






Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

computer guy

Dear Mrs Bob zhu - MSFT,

Sorry I didn’t mention that I am working with visual basic. Net

I think your code is in C++ or C#

Because it didn’t work in my program.

Thank you





Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Bob zhu - MSFT

for vb code, you can try follow

but the thing I am afraid is I am talking about winform, I am not sure if you need winform code

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim tb As DataTable = New MyGenTable().get_TBA() 'here we just generate a datatable

Me.dataGridView1.AutoGenerateColumns = False

Me.dataGridView1.DataSource = tb

Dim tc1 As New DataGridViewTextBoxColumn()

tc1.DataPropertyName = "ID"

Me.dataGridView1.Columns.Add(tc1)

Dim style As New DataGridViewCellStyle()

style.SelectionBackColor = Color.Red

Me.dataGridView1.Columns(0).DefaultCellStyle = style

End Sub 'button1_Click






Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

computer guy

Dear friend,

Thank you for being patient to me.

I tried the code you posted here, but it also did not work.

This is what I am doing:

Using visual basic in windows forms, I am doing a project on (university students advisory), one of the features is to allow the student to choose from some courses, and display it on the time table.

The time table is like this : (the rows are the days and the columns are the time divided into half hours) “I wanted to post print screens of the forms, but I couldn’t”.

When the student chooses a course, the corresponding cells should get new color (green).

I need those green colors to remain permanent, but the problem is:

When I click on the columns header it is ordering the content of the table, and the colors are gone.

This problem occurs even if I set those properties:

DataGridView1.AllowDrop = False

DataGridView1.AllowUserToOrderColumns = False

DataGridView1.AllowUserToResizeRows = False

DataGridView1.AllowUserToResizeColumns = False

DataGridView1.AllowUserToAddRows = False

DataGridView1.AllowUserToDeleteRows = False

DataGridView1.ReadOnly = True

I created the grid using the tool box.

I am using Microsoft Access to create the data base, I called the table StudentTable, so I created the table as it is shown , but I kept it empty , then I wrote in the form load event this code:

Dim dt As DataTable = New DataTable

sql = "Select * from StudentTable ;"

da = New OleDb.OleDbDataAdapter(sql, conn)

da.Fill(dt)

studentTable.DataSource = dt

I hope that would be clear, and thanks a lot again.





Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Bob zhu - MSFT

May be you miss something like connection string in your code,

follow is my demo, it works in my pc, please try it

Imports System.Data.OleDb

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim dt As DataTable = New DataTable

Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\db.mdb")

Dim Sql As String = "SELECT Table1.* FROM Table1 ;"

Dim da As Data.OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(Sql, conn)

da.Fill(dt)

studentTable.DataSource = dt

End Sub

End Class






Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

computer guy

Dear friend,

I think with the pictures bellow the problem will be clearer.

I did all of that, and the table is coming as you can see in this link:

http://www.savefile.com/files/602313

and it is correctly colored as you can see in this link:

http://www.savefile.com/files/602322

The connection was done successfully. But when I put the mouse on the header as shown in the picture in the link below, orange color appears in bottom of the header, and if I click on it, the colors goes, and the grid returns as it was before.

“The header clicking event is ordering the columns content; I don’t want the user to be able to order the columns content”

http://www.savefile.com/files/602425

Thanks





Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Bob zhu - MSFT

Hi guy , as your pic shows you want color more than selected cell, so I think we can try redraw cells to color it

follow I draw backcolor of cell[1,1] to green,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.DataGridView1.Rows.Add(New DataGridViewRow())

AddHandler Me.DataGridView1.CellPainting, AddressOf dataGridView1_CellPainting

End Sub

Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)

If e.RowIndex = 1 And e.ColumnIndex = 1 Then

e.Graphics.FillRectangle(New SolidBrush(Color.Green), e.CellBounds)

e.Handled = True

End If

End Sub 'dataGridView1_CellPainting

End Class






Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

computer guy

Thank you very much friend,

I really appreciate it.

Can you explain this code to me, please

Is this a function call How it works

AddHandler Me.DataGridView1.CellPainting, AddressOf dataGridView1_CellPainting

Can I put this code in a button click event or any other events Or it is like a function, which should be separately written

If e.RowIndex = 1 And e.ColumnIndex = 1 Then

e.Graphics.FillRectangle(New SolidBrush(Color.Green), e.CellBounds)

e.Handled = True

End If

This way of redrawing the cell is working, but there are two problems:

- the colour is hiding the cell borders, then when the student cancel that course from the table, the cell colour should return to white, when I use the same code to colour it to white, the borders are not there.

http://www.savefile.com/files/611968

- I have many grids in my application, as you see in the following picture.

http://www.savefile.com/files/611962

Plenty of the cells have values and words written to them, and if I use this approach the values are not visible.

I still can use this code in colouring the time table, where the colour is the most important value.

But I will search tell I find a way to let the back ground colour not changeable.” I think there must be a way”.

Maybe there is another tool to display the data and colour the background

Till we find one, I have an idea, it may help:

I think the back colour is changing because the columns are generated by the grid, but if we create the columns by the code, there will not be the orange bar that allows the user to arrange the content of the column. Something like the code bellow (I tried it, but I couldn’t generate the rows if you can help me with)

Dim col1 As New DataGridViewColumn

Col1.Name = "10:30"

dataGridView1.Columns.Add(col1)





Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Bob zhu - MSFT

Hi, the code above just add handler to the cellpainting event, it is equal to add handler in the property view in design time

for newxt question, I think yes, you can add handler anywhere you like, in button click is ok, my demo is in button click too.

for cell border you can add one line as brown, and fore string as line green,

Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)

If e.RowIndex = 1 And e.ColumnIndex = 1 Then

e.Paint(e.CellBounds, DataGridViewPaintParts.Border)

e.Graphics.FillRectangle(New SolidBrush(Color.Green), e.CellBounds)

e.Graphics.DrawString(Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString(), Me.Font, Brushes.Black, e.CellBounds)

e.Handled = True

End If

End Sub 'dataGridView1_CellPainting

it will paint border for you.

may be you can add a tag to identify what color the specified cell need, and judge it in the cellpaint event,






Re: Windows Forms Data Controls and Databinding How to make the back color of the cells permenent?

Computer guy

Mrs. Bob zhu,

Sorry for this delay, but I was not able to write any thing on the forums for three days, and I don’t know why!

I tried your code, it was working, but I found some thing easier.

The problem was because of sorting property, since I don’t want the user to sort the contents I searched for disabling that property and I found this:

'to prevent the user from sorting the columns

For i = 0 To DataGridView1.Columns.Count - 1

DataGridView1.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable

Next

It is true that I troubled you with me, but I learned a lot and I really appreciate it.

Thank you

Computer Guy