Home » C# ProgrammingRSS

How to add checkbox and dropdownlist controls to DataGrid

Hi Guys,

May I know how can I add checkbox and dropdownlist controls to datagrid control at runtime in VS 2005 for windows mobile based device?

I run the SQL query to pupolate data into the datagrid at runtime, but then I do not know how to customise the datagrid to insert checkbox for a value in my column which would either be Yes or No (boolean), and that I would like to make it into a checkbox column, if the value is Yes, then the checkbox is checked, else it's unchecked.

Also, if my value for that checkbox is null, how can I make all the row with the checkbox checked at runtime?

Thanks.

Regards,
Jenson
 

27 Answers Found

 
 

Answer 2

Hi Ilya,

Thanks for the reply. However, is the link that yuo pointed to me related to odd and even rows formating and some new features of the datagrid  in NET CF v2. However, I did not see anything talking about checkbox  and dropdownlist?

Btw, I'm thinking of trying the possibilities of using ListView to replace datagrid too, since datagrid in NET CF is so limited. But I have concerns about issues arising from the data readability, functionality, programmatically and practical use of it.

Please advise and enlighten me on this, would you?

Thanks.

Regards,
Jenson
 

Answer 3

Sorry Ilya, my mistakes, I overlooked some part, it did have samples on checkbox  and combobox, i would need to convert it into VB.NET before I can use it on VS 2005, I supposed, it should be the same in coding it in VS 2005?

Please pardon me for my stupid questions =)

Regards,
Jenson (from the dumb side of .NET world)
 

Answer 4

Ilya Tumanov wrote:
See this.



Hi Ilya,

It does look very complicated and require some work on it but I think it might be worthwhile, however, I'm still interested in exploring the possibilities to replace datagrid  with listview, waiting for your expertise on this =)

Thanks for always willing to answer to my questions.

Regards,
Jenson
 

Answer 5

Ilya Tumanov wrote:
See this.



Hi Ilya,

I have not problem in comprehending C#, but I do have problem in rewriting them into reusable VB codes.

Regards,
Jenson
 

Answer 6

Hi Illya,

I have managed to convert C# codes into VB.NET code here: http://labs.developerfusion.co.uk/

A nice convertor, converted almost all of the codes for you.

PS: Anyone who would like a nice convertor, please try the link on top, it can do two ways conversion, from C# to VB and vice versa.

Thanks again.

Regards,
Jenson
 

Answer 7

Hi Ilya,

I think the checkbox  somehow is not working properly, I don't really know where went wrong, it just constantly display as "X" and I can't seem to be able to check it at all. Also, the combobox, once selected, it will always get the focus and I can't click on elsewhere of the datagrid.

Please advise where I make mistake?

Thanks.

Regards,
Jenson
 

Answer 8

I'll just let you to find it on your own as you seem to be fully capable of investigating and fixing your own mistakes. You can always see how original sample works if you need a hint.

 

Answer 9

HI Ilya,

Thanks for the compliment. Though I might be able to spot my problems and take a look at how original pieces work out to achieve what I want, I'm still having some problems there. But I will try my best to comprehend and find out where I made a mistake or some mistakes.

Of course I would prefer to write such a beautiful class projects to be reuse in the future projects, but that's beyong my capabilities, unless I would have the chance to dig deeper into writing one, which I always wish to.

Btw, just allow me to OT a bit, do you have any link or resources to start writing like a C# class file (to me, it's almost the same like Java class) in VB or so, as a beginner in this area? I have experience (not much though) in writing VB codes and some Java codes, but I have no experience in such an elegantly written classes, or can I call it interface? Sorry I'm not good in this.

Regards,
Jenson
 

Answer 10

Ilya Tumanov wrote:

I'll just let you to find it on your own as you seem to be fully capable of investigating and fixing your own mistakes. You can always see how original sample works if you need a hint.



Hi Ilya,

I think I still see the one working weirdly even though much tuning has been done. I'm still don't know why the problem still persists. It sure cannot be due to I only have one row of record retrieved (I created in such way for testing), I load everything from a sqlce database, and in fact only one row meet the SQL query criteria and populate into DataGrid control. I still don't know why my checkbox  cannot be checked or uncheck, it always remain as "X". Same for my combobox, once I selected an item from the list, it always get highlighted and selected, I can't even move away from it even though I try to click everywhere of the form.

Could you please enlighten a bit? You don't have to show me the codes if you think it isn't necessasy, just explain to me what the possible causes and ways to troubleshoot or solve it?

Thanks.

Regards,
Jenson
 

Answer 11

At least, the combobox is working fine now, just that, despite showing me the correct value for both of my custom checkbox  control, they can't be checked or unchecked. And I actually make use of a combobox on the form to display data based on two different criteria, once I choose another item in the combobox after showing the data on datagrid  of the first criteria, it just return me error, it seems like the datagrid cannot be redrawn as there is already control  drawn on the datagrid, I wonder how I can redraw it so that the second criteria can populate dataa to the datagrid and remove all the previous records retrieved based on the first criteria selected from the combobox.

They work fine before the custom controls  were implemeted.

Regards,
Jenson
 

Answer 12

Btw, Ilya, I can always create the customcontrol class for each control  in the same project right? all the class files are in the same project, and those forms that need custom control in datagrid  would have additional imports statement, which is

Code Snippet

Imports Project1.DataGridCustomColumns



Which Project1 is the name I call my project when I created it.
 

Answer 13

Btw, these are my codes for the form:

Code Snippet

Public Class frmClass

    Dim dsGetData As DataSet = Nothing
    Dim dtGetData As DataTable
    Dim dtGetLateCode As DataTable

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

        Cursor.Current = Cursors.Default

        checkAccessLvl(gUserAccessLvl, mnuCAAdmin)
        fillClass(gUserID)

    End Sub

    Private Sub SetupTableStyles()
        Dim alternatingColor As Color = SystemColors.ControlDark

        Dim dtGetData As DataTable = Me.dsGetData.Tables(0)

        ' ID Column

        Dim dataGridCustomColumn0 As New DataGridCustomTextBoxColumn()

        With dataGridCustomColumn0
            .Owner = Me.dtgCAStudent
            .FormatInfo = Nothing
            .HeaderText = dtGetData.Columns(0).ColumnName
            .MappingName = dtGetData.Columns(0).ColumnName
            .Width = dtgCAStudent.Width * 10 / 100         ' 10% of the grid size
            .AlternatingBackColor = alternatingColor
            .ReadOnly = True
        End With

        DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn0)

        ' Student Name column

        Dim dataGridCustomColumn1 As New DataGridCustomTextBoxColumn()

        With dataGridCustomColumn1
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(1).ColumnName
            .MappingName = dtGetData.Columns(1).ColumnName
            .NullText = ""
            .Width = Me.dtgCAStudent.Width * 40 / 100 ' 40%
            .Alignment = HorizontalAlignment.Right
            .AlternatingBackColor = alternatingColor
            .ReadOnly = True
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn1)

        ' Student Identity Number column

        Dim dataGridCustomColumn2 As New DataGridCustomTextBoxColumn()

        With dataGridCustomColumn2
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(2).ColumnName
            .MappingName = dtGetData.Columns(2).ColumnName
            .NullText = ""
            .Width = Me.dtgCAStudent.Width * 20 / 100 ' 15%
            .Alignment = HorizontalAlignment.Left
            .AlternatingBackColor = alternatingColor
            .ReadOnly = True
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn2)

        ' Attendance column

        Dim dataGridCustomColumn3 As New DataGridCustomCheckBoxColumn()

        With dataGridCustomColumn3
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(3).ColumnName
            .MappingName = dtGetData.Columns(3).ColumnName
            .NullText = "-Unknown-"
            .Width = Me.dtgCAStudent.Width * 10 / 100 ' 10%
            .Alignment = HorizontalAlignment.Left
            .AlternatingBackColor = alternatingColor
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn3)

        ' Late column

        Dim dataGridCustomColumn4 As New DataGridCustomCheckBoxColumn()

        With dataGridCustomColumn4
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(4).ColumnName
            .MappingName = dtGetData.Columns(4).ColumnName
            .NullText = "-Unknown-"
            .Width = Me.dtgCAStudent.Width * 10 / 100 ' 10%
            .Alignment = HorizontalAlignment.Left
            .AlternatingBackColor = alternatingColor
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn4)

        ' Late Code column

        Dim dataGridCustomColumn5 As New DataGridCustomComboBoxColumn()

        With dataGridCustomColumn5
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(5).ColumnName
            .MappingName = dtGetData.Columns(5).ColumnName
            .NullText = ""
            .Width = Me.dtgCAStudent.Width * 30 / 100 ' 30%
            .Alignment = HorizontalAlignment.Center
            .AlternatingBackColor = alternatingColor
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn5)

        ' Remarks column

        Dim dataGridCustomColumn6 As New DataGridCustomTextBoxColumn()

        With dataGridCustomColumn6
            .Owner = Me.dtgCAStudent
            .HeaderText = dtGetData.Columns(6).ColumnName
            .MappingName = dtGetData.Columns(6).ColumnName
            .NullText = ""
            .Width = Me.dtgCAStudent.Width * 30 / 100 ' 30%
            .Alignment = HorizontalAlignment.Left
            .AlternatingBackColor = alternatingColor
        End With

        Me.DataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn6)

        Me.DataGridTableStyle1.MappingName = dtGetData.TableName                    ' Setup table mapping name

        Me.dtgCAStudent.DataSource = dtGetData                                      ' Setup grid's data source

        Dim cboLateCode As ComboBox = CType(dataGridCustomColumn5.HostedControl, ComboBox)

        Dim dtGetLateCode As DataTable = Me.dsGetData.Tables(1)                         ' Set up data source

        cboLateCode.DataSource = dtGetLateCode                                          ' For combo box column
        cboLateCode.DisplayMember = dtGetLateCode.Columns(0).ColumnName
        cboLateCode.ValueMember = dtGetLateCode.Columns(0).ColumnName

        'Me.dtgCAStudent.CurrentRowIndex = 50                                ' Move to the middle of the table

    End Sub

    Private Sub fillClass(ByVal userIDNo As Integer)

        Dim connGetClass As SqlCeConnection = Nothing
        Dim cmdGetClass As SqlCeCommand
        Dim connStrGetClass As String
        Dim drGetClass As SqlCeDataReader

        connStrGetClass = gDataSource

        Try

            connGetClass = New SqlCeConnection
            connGetClass.ConnectionString = connStrGetClass

            cmdGetClass = New SqlCeCommand("SELECT * FROM tblClass WHERE TeachInChg1 = " & gUserID & " OR TeachInChg2 = " & gUserID, connGetClass)

            connGetClass.Open()

            drGetClass = cmdGetClass.ExecuteReader

            While drGetClass.Read

                If (Not (IsDBNull(drGetClass("TeachInChg1")))) Or (Not (IsDBNull(drGetClass("TeachInChg2")))) Then

                    cboCAClass.Items.Add(drGetClass![ClassCode])

                End If

            End While

            drGetClass.Close()
            cmdGetClass.Dispose()

        Catch ex As Exception

            MsgBox("" & ex.Message, MsgBoxStyle.Information, "Error")
            'MsgBox("No class found under your name.", MsgBoxStyle.Information, "Error")
            cboCAClass.Focus()
            Exit Sub

        Finally

            connGetClass.Close()

        End Try

    End Sub

    Private Sub mnuCAExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCAExit.Click

        Cursor.Current = Cursors.WaitCursor
        frmAttendance.Show()
        Me.Close()

    End Sub

    Private Sub mnuCAAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCAAdmin.Click

    End Sub

    Private Sub mnuCAMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCAMainMenu.Click

        Cursor.Current = Cursors.WaitCursor
        frmMain.Show()
        Me.Close()

    End Sub

    Private Sub mnuCAAMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCAAMenu.Click

        Cursor.Current = Cursors.WaitCursor
        frmAttendance.Show()
        Me.Close()

    End Sub

    Private Sub mnuCAExtClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCAExtClass.Click

        Cursor.Current = Cursors.WaitCursor
        frmExtraClass.Show()
        Me.Close()

    End Sub

    Private Sub mnuCACAAttendance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCACAAttendance.Click

        Cursor.Current = Cursors.WaitCursor
        frmCCA.Show()
        Me.Close()

    End Sub

    Private Sub mnuCASubjAttendance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCASubjAttendance.Click

        Cursor.Current = Cursors.WaitCursor
        frmSubject.Show()
        Me.Close()

    End Sub

    Private Sub cboCAClass_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCAClass.TextChanged

        Dim selValue As String

        selValue = cboCAClass.Text

        Cursor.Current = Cursors.WaitCursor

        displayClassInfo(selValue)
        displayData(selValue)

        SetupTableStyles()

    End Sub

    Private Sub displayData(ByVal selClass As String)

        Dim connGetData As SqlCeConnection = Nothing
        Dim cmdGetData As SqlCeCommand
        Dim cmdGetLateCode As SqlCeCommand
        Dim connStrGetData As String
        Dim daGetData As SqlCeDataAdapter
        Dim daGetLateCode As SqlCeDataAdapter

        dtGetData = New DataTable("Student")
        dtGetLateCode = New DataTable("LateCode")

        dsGetData = New DataSet
        dsGetData.Tables.Add(dtGetData)
        dsGetData.Tables.Add(dtGetLateCode)

        dtGetData.Columns.Add("ID", GetType(Int32))
        dtGetData.Columns.Add("Name", GetType(String))
        dtGetData.Columns.Add("NRIC/FIN", GetType(String))
        dtGetData.Columns.Add("Attd", GetType(Boolean))
        dtGetData.Columns.Add("Late?", GetType(Boolean))
        dtGetData.Columns.Add("Reason", GetType(String))
        dtGetData.Columns.Add("Remarks", GetType(String))

        connStrGetData = gDataSource

        Try

            connGetData = New SqlCeConnection
            connGetData.ConnectionString = connStrGetData

            cmdGetData = New SqlCeCommand("SELECT RegistryNo AS ID, StudName AS Name, StudICNo AS ""NRIC/FIN"", Attendance as Attd, Late as ""Late?"", LateID as Reason," & _
                                          "Remarks FROM (tblStudent INNER JOIN tblStudClass ON tblStudent.IDNo = tblStudClass.StudID) " & _
                                          "INNER JOIN tblClass ON tblClass.IDNo = tblStudClass.ClassID LEFT OUTER JOIN tblStudClassAttd " & _
                                          "ON tblStudClassAttd.StudClassID = tblStudClass.IDNo WHERE tblStudent.Status = 'A' and " & _
                                          "tblClass.ClassCode ='" & selClass & "'", connGetData)

            connGetData.Open()

            daGetData = New SqlCeDataAdapter(cmdGetData)

            daGetData.Fill(dsGetData, "Student")

            dtgCAStudent.DataSource = dsGetData.Tables("Student")


            'Get the Late Code from tblSYLegend and populate them into the custom combobox control

            cmdGetLateCode = New SqlCeCommand("SELECT Code FROM tblSYLegend WHERE Type = 1 AND Seq > 0", connGetData)

            daGetLateCode = New SqlCeDataAdapter(cmdGetLateCode)

            daGetLateCode.Fill(dsGetData, "LateCode")

        Catch ex As Exception

            MsgBox("" & ex.Message, MsgBoxStyle.Critical, "Error")
            Exit Sub

        Finally

            Cursor.Current = Cursors.Default

            connGetData.Close()

        End Try

    End Sub

    Private Sub displayClassInfo(ByVal selectedClass As String)

        Dim connGetInfo As SqlCeConnection = Nothing
        Dim cmdGetInfo As SqlCeCommand
        Dim connStrGetInfo As String
        Dim drGetInfo As SqlCeDataReader

        connStrGetInfo = gDataSource

        Try

            connGetInfo = New SqlCeConnection
            connGetInfo.ConnectionString = connStrGetInfo

            cmdGetInfo = New SqlCeCommand("SELECT C.Stream, T1.TeachName AS Teacher1, T2.TeachName AS Teacher2 " & _
                                          "FROM (tblClass C INNER JOIN tblTeacher T1 " & _
                                          "ON C.TeachInChg1 = T1.IDNo) " & _
                                          "INNER JOIN tblTeacher T2 " & _
                                          "ON C.TeachInChg2 = T2.IDNo " & _
                                          "WHERE ClassCode ='" & selectedClass & "'", connGetInfo)

            connGetInfo.Open()

            drGetInfo = cmdGetInfo.ExecuteReader

            While drGetInfo.Read

                If (Not (IsDBNull(drGetInfo("Stream")))) Or (Not (IsDBNull(drGetInfo("Teacher1")))) Or (Not (IsDBNull(drGetInfo("Teacher2")))) Then

                    txtCAStream.Text = Trim(drGetInfo![Stream])
                    txtCAFormTeacher.Text = Trim(drGetInfo![Teacher1])
                    txtCACoForm.Text = Trim(drGetInfo![Teacher2])

                End If

            End While

            drGetInfo.Close()
            cmdGetInfo.Dispose()

        Catch ex As Exception

            MsgBox("" & ex.Message, MsgBoxStyle.Information, "Error")
            cboCAClass.Focus()
            Exit Sub

        Finally

            connGetInfo.Close()

        End Try


    End Sub

End Class


 

Answer 14

Ok, it's working fine now, except for the checkbox, even though the data captured is Boolean True, but the box still appears as "X", same thing, can't check or uncheck the box still. Hmmm, fingers crossed.
 

Answer 15

Just for some updates, the checkbox  is working but it work the other way round, it won't shown up when the datagrid  is loaded, it only show up when you click on the checkbox, but it won't change the value until you click on it for changes. Hmm, a bit strange?

Regards,
Jenson
 

Answer 16

The real check box (or any other hosted control) only shows after cell with it is selected. So that sounds about right.

 

Answer 17

Hi Ilya,

Alright, that clears my doubts =)

Thanks Ilya for the great solutions to customized controls, but it does require one to put in efforts to fine tune and play around with it.

Regards,
Jenson
 

Answer 18

Which is exactly the point of a sample…

 

Answer 19

Hi Ilya,

I have another question, how do I dispose the grid and the custom controls  upon clicking on a custom close button? When the user close the form, he/she will be redirected to the login page, if he login again, he should not be able to see the controls on the grid again.

however, when I check on one checkbox  control, and close the form, and login again, the grid is empty but loaded with the previously checked checkbox. May i know how can I fix this?

EDIT: Or to put it simple, when user close the form or navigate away (load another form and close this form), after that load back the same form again, he/she should not see anything on the grid, but in my case, they still see it. That's strange even though I've issued a DataGridTableSyle1.GridColumnStyles.Clear() to the form upon closing. Simply put, the form should redraw itself with empty box, which does not contain any grid at all, or any table style.

Thanks.

Regards,
Jenson
 

Answer 20

Btw, Iilya, I wanted to save the changes made on the grid, but i'm using normal SQL statement, and use the code to loop through every row and every columns, and verify them one by one against the table to check whether the same record exists, if yes, then update it, else insert as new record, however, I found that, even though I unchecked or checked the checkbox, it will not get updated, instead, it maintain its initial value, let's say 1 (checkbox is checked), even though I have unchecked it (so it should be 0 now), but it still get 1 when the record is being updated or inserted.

Would you be kind enough to explain this to me again? How can I get the changes from the grid and custom columns without having to use dataadapter? If I must use dataadapter, how am i going to implement one to update it?

Thanks,

Regards,
Jenson
 

Answer 21

Ilya Tumanov wrote:

Which is exactly the point of a sample…



Hi Ilya,

It seems that your sample would show the checkbox  correctly even though the columns are not getting the focus, may  I know how can achieve what it is shown in the sample codes?

I've try many ways, but in the end all fails to create the custom checkbox like what it
s shown in the sample implementation of DataTest.

I don't think I have to do exactly the same like what you've done in the sample right? I think I can always include that 4 files in the project and making use of them, isn't it?

I don't think it will only show up if the current column of checkbox get the focus, as this is not the case in your given sample.

Please enlighten me.

Thanks.

Regards,
Jenson
 

Answer 22

Alright, everything work perfectly well after I import the sample and use it on my project. Weird, btw, does this has any copyright or licensing issues if I use it?

Thanks.

Regards,
Jenson
 

Answer 23

The textbox doesn't seems to be working quite well in actual device, though it works perfectly well in Windows Mobile 5.0 Emulator. Hmm, I wonder why there is such a huge different.

Whenever I click on the textbox it gives me a NullReferenceException error and point me to a Designer.VB of a particular form, and it's always the same form, on the declaration Partial Public Class Form1, it stated that NullReferenceException was unhandled.

Troubleshooting Tips:

1. Use the "new" keyword to create an object instance (hm, I never put any codes to call out any form when a custom textbox is clicked, moreover, this is a designer.vb file, why there's a need to use "new" there

2. Check to determine if the object is null before calling the method (again, I never call the form through the custom textbox).

3. Get general help for this exception (not sure what is this).

Anyone can guide me through? Or Ilya?

Thanks.

Regards,
Jenson
 

Answer 24

After been playinga around with the sample codes and changing it here and there, I have a problem here, maybe Ilya can help to clear my doubts a tad.

I'm wondering how can I disable the alternating row color of rows in custom datagrid? I kept on trying for one whole day but I can't get all rows to remain as white for their backcolour and black text. That's quite weird.

Ilya or anyone can guide me a bit here?

Thanks.

Regards,
Jenson
 

Answer 25

Hi All,

Sorry, I have found the solution, this is what I've done:

First I looked for this block of codes:

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush)
{
Brush background = backBrush; // Use default brush by... hmm... default.

if((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum))
{ // If have alternating brush, row is odd and not selected...
background = _alternatingBrush; // Then use alternating brush.
}

g.FillRectangle(background, bounds); // Draw cell background
}


Then I commented some portion of it and make it looked like this:

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush)
{
Brush background = backBrush; // Use default brush by... hmm... default.

//if((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum))
//{ // If have alternating brush, row is odd and not selected...
// background = _alternatingBrush; // Then use alternating brush.
//}

g.FillRectangle(background, bounds); // Draw cell background
}

The other parts involved are quite similar too.

Thanks.

Regards,
Jenson
 

Answer 26

Hi Ilya:

http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesvbcs/thread/04d204ba-701c-48a8-97c1-4be6f19132a1/

The reference url is above.Your code is not bad.But how can I show the real checkbox  of the unselected cells.

David

Many thanks.

 

Answer 27

Hi All,

Sorry, I have found the solution, this is what I've done:

First I looked for this block of codes:

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush)
{
Brush background = backBrush; // Use default brush by... hmm... default.

if((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum))
{ // If have alternating brush, row is odd and not selected...
background = _alternatingBrush; // Then use alternating brush.
}

g.FillRectangle(background, bounds); // Draw cell background
}


Then I commented some portion of it and make it looked like this:

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush)
{
Brush background = backBrush; // Use default brush by... hmm... default.

//if((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum))
//{ // If have alternating brush, row is odd and not selected...
// background = _alternatingBrush; // Then use alternating brush.
//}

g.FillRectangle(background, bounds); // Draw cell background
}

The other parts involved are quite similar too.

Thanks.

Regards,
Jenson

I also had that issue, i converted the custom controls  to vb.net 2008, this is what i've done  to correct the problem with the alternativecolor cell selection

 

If (background IsNot Nothing) AndAlso ((rowNum And 1) <> 0) AndAlso rowNum <> Owner.HitTest(Control.MousePosition.X - Owner.Left, Control.MousePosition.Y - Owner.Top).Row Then

By correctly checking if the cell that one is _trying_ to select is the row we want to select. DataGrid only identifies a selected cell after the selection is done, not before. Though, the checking for Owner.isSelected() will always return false. The alternative isto use the HitTest()

 

Hopefully it will help someone else :)

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter