Sign in to follow this  

[.net] Simple table in C#

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to figure out an easy way to display a simple table in VS.NET with C#. The table basically has 4 colums and 10 rows. The data in the cells will change, but it's not loaded from a database or something. More like set a default value, then click a button and the value in a cell changes. One solution is to do it all with labels, but that will be tedious and not very easy to change down the road. I also know the DataGrid object would work, but that does a little more than I need, and won't do other stuff I will need. The data is a series of tests, with the outcome filling the cells of a test for a given row. I would like to do things like change the background of the cell if there is an error with that test. However, DataGrids don't seem to give you that kind of control. They just have two colors that are even or odd rows and spread all the way across. I want more controll over formating of a single cell (like you would be able to in HTML). Does anyone know of anything that might work for me?

Share this post


Link to post
Share on other sites
Quote:

They just have two colors that are even or odd rows and spread all the way across.

You may need to look at DataGridTablestyle as well, but everything you have described can be done using DataGrids and the associated classes. i.e. DataTable, DataColumn etc.

Share this post


Link to post
Share on other sites
I have looked at the DataGrid and DataGridstyle objects, and they do most of what I need to do. They can organize the data in a table format, but I need more control over how individual cells are drawn.

I can't find anything in DataGrid or DataGridstyle that lets you define how a single cell should be drawn (ie with a different background color). The only options are to set the colors for even and odd number rows. DataGridstyle does let you control a lot of parameters, but only for the whole table, not individual items. I need something more like an html based table where I can define in each cell how it looks.

Share this post


Link to post
Share on other sites
Well one way to do it is to use the DataGridTextBoxColumn. Note: This probably is not the best method and I'm certain that it is not the only method.

At one point I needed custom font colors in each cell based on the data it contained. So I created a new class that inherited DataGridTextBoxColumn. Once you do that you can the add properties to store the info you need. Like new font color, back ground color, conditions to look for etc. Last you will override the Pain Event. Paint gives you the current row that is being drawn so it is just simple matter to look at the dataset and get the data, compare that to your conditions and draw as necessary.

Here is some sample VB.Net Code for how you might go about making the new class... this is just to give you the general idea so don’t try to just cut and paste as it likely wont even compile. This should easily translate in to C#

Public Class DataGridTextBoxColumnPlus2
Inherits System.Windows.Forms.DataGridTextBoxColumn
Private m_DataSource As DataTable
Private m_SpecialColors As New Collection()

Public Sub AddSpecialDisplay(ByVal idColumn As String, ByVal idValue As String, ByVal TextColor As Color, ByVal BackColor As Color)
Dim MySpecial = New SpecialColor()
MySpecial.IdentifingColumnName = idColumn
MySpecial.IdentifingValue = idValue
MySpecial.AltRowColor = BackColor
MySpecial.AltTextColor = TextColor
m_SpecialColors.Add(MySpecial)
'MySpecial = Nothing
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim iLoop As Integer
Try
For iLoop = 1 To m_SpecialColors.Count
If Not (m_DataSource Is Nothing) And (m_SpecialColors.Item(iLoop).IdentifingColumnName <> "") Then
If (rowNum <= m_DataSource.Rows.Count) And (rowNum > -1) Then
If m_DataSource.Rows(rowNum).Item(m_SpecialColors.Item(iLoop).IdentifingColumnName).ToString.ToUpper = m_SpecialColors.Item(iLoop).IdentifingValue.ToUpper Then
If Not m_SpecialColors.Item(iLoop).AltTextColor.Equals(Color.YellowGreen) Then
foreBrush = New SolidBrush(m_SpecialColors.Item(iLoop).AltTextColor)
End If
If Not m_SpecialColors.Item(iLoop).AltRowColor.Equals(Color.YellowGreen) Then
backBrush = New SolidBrush(m_SpecialColors.Item(iLoop).AltRowColor)
End If
End If
End If
End If
Next
Catch ex As Exception
Finally
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub








VB.Net Code to use the new class

Private Sub SetupResultGrid(ByVal strTableName As String)
Dim Tablestyle As DataGridTablestyle = New DataGridTablestyle
Dim Column As DataColumn
Dim Columnstyle As DataGridTextBoxColumnPlus

Try
'clear the tablestyle
DatResults.Tablestyles.Clear()
'assign the new table
Tablestyle.MappingName = strTableName

'Format the columns
Columnstyle = New DataGridTextBoxColumnPlus
With Columnstyle
.ReadOnly = True
.TextBox.Enabled = False
.HeaderText = "Client ID"
.MappingName = "ClientIDText"
.Width = 75
.AltTextColor = Color.Crimson
.IdentifingValue = "InActive"
.IdentifingColumnName = "LookupDesc"
.SetDataSource(DatResults.DataSource)
End With
Tablestyle.GridColumnstyles.Add(Columnstyle)

'setup the dbgrid to look nice
With Tablestyle
.AllowSorting = False
.AlternatingBackColor = System.Drawing.Color.Lavender
.BackColor = System.Drawing.Color.WhiteSmoke
.ForeColor = System.Drawing.Color.MidnightBlue
.GridLineColor = System.Drawing.Color.Gainsboro
.GridLinestyle = System.Windows.Forms.DataGridLinestyle.None
.HeaderFont = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.Fontstyle.Bold)
.HeaderForeColor = System.Drawing.Color.Black
.LinkColor = System.Drawing.Color.Teal
End With

'apply the style changes to the Datagrid
DatResults.Tablestyles.Add(Tablestyle)
'cleanup
Columnstyle = Nothing
Tablestyle = Nothing
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub







Edit: source tags

Share this post


Link to post
Share on other sites

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this