Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Is it possible to dynamically set DataGrid Column Styles

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guy Thornton - 20 Jul 2006 21:15 GMT
Hello everyone,

I am really stumped about how to accomplish this and I am looking for
suggestions, and opinions on the best way to handle this scenario:

I am writing a Questionnaire application in VS 2005, using .NET 2.0.

The form uses a datagrid to display questions and answers on it.  I have a
Question Object and a Questionnaire object defined.  The Questionnaire Object
has a collection of Question objects.  The Questionnaire.questions method
returns my collection, which I set as the datasource of my datagrid.  I have
also used a binding source wrapper to my Question object to access the
methods I want displayed in the datagrid.  So far so good.  All is working
well.

The problem I want to solve is that the Question Objects contain a
QuestionType object.  one of the specific types is a YesNoQuestionType.  
Another QuestionType is a FreeTextQuestionType.

I want my datagrid to display the appropriate ColumnStyle for each question
displayed.  So If a FreeText Question in the list, it displays a textbox to
edit the answer for.  If I have a Yes No question, it should display a
combobox column that I can choose Yes or No as the answer.

Like I said, I don't even know if this is possible, but if it is, please let
me know.

Thanks
Brian Tkatch - 20 Jul 2006 21:30 GMT
> Hello everyone,
>
[quoted text clipped - 24 lines]
>
> Thanks

It is possible.

Private Sub Create_DataGrid_Column_Common(ByVal Column As
DataGridColumnStyle, ByVal Name As String, ByVal Header As String)
       Column.MappingName = Name
       Column.HeaderText = Header
End Sub

Public Function Create_DataGrid_Column_TextBox(ByVal Name As String,
ByVal Header As String) As DataGridTextBoxColumn
       Create_DataGrid_Column_TextBox = New DataGridTextBoxColumn
       Create_DataGrid_Column_Common(Create_DataGrid_Column_TextBox,
Name, Header)
End Function

Public Function Create_DataGrid_Column_Bool(ByVal Name As String,ByVal
Header As String) As DataGridBoolColumn
       Create_DataGrid_Column_Bool = New DataGridBoolColumn
       Create_DataGrid_Column_Bool.FalseValue = "No"
       Create_DataGrid_Column_Bool.TrueValue = "Yes"
       Create_DataGrid_Column_Common(Create_DataGrid_Column_Bool,
Name, Header)
End Function

Then:

Dim Data_Grid_Table_Style As New DataGridTableStyle

       Data_Grid_Table_Style.MappingName = ...

       Data_Grid_Table_Style.GridColumnStyles.AddRange _
       ( _
        New DataGridColumnStyle() _
        { _
         Create_DataGrid_Column_TextBox("Sourcename", "Display name"),
_
         Create_DataGrid_Column_Bool("Sourcename", "Display name") _
        } _
       )

Or something like that.

BTW, if you are developing this in 2005, the appropriate .NET 2.0
object is DataGridView, not DataGrid. DataGrid is a .NET 1.0 object
supported for backwards compatability.

B.
Guy Thornton - 21 Jul 2006 03:54 GMT
Brian,

Thanks for this information I will begin working on getting it implemented
in my project.  One question, should this code get implemented in the form
that contains the DataGridView object?

> > Hello everyone,
> >
[quoted text clipped - 72 lines]
>
> B.
Brian Tkatch - 21 Jul 2006 14:36 GMT
> Brian,
>
[quoted text clipped - 78 lines]
> >
> > B.

No. Best in another module, depending on how its called.

Anyway, if it is a DataGridView (as opposed to a DataGrid) this code
will not work.For a DataGridView there are no column styles, and
instead there are Columns collections, but they cannot be disassociated
from the DatGridView. Even the constructor requires the DataGridView it
is associated with.

Instead, you could use the following: (I'm still messing with it, so
this isn't complete.)

Private Sub Create_Column_Common(ByVal Column As DataGridViewColumn,
ByVal Name As String, ByVal Header As String, ByVal Read_Only As
Boolean)
       With Column
           .Name = Name
           .HeaderText = Header
           .ReadOnly = Read_Only
           .DataPropertyName = Name
       End With
End Sub

Public Function Create_Column_TextBox(ByVal Name As String, ByVal
Header As String, Optional ByVal Read_Only As Boolean = False) As
DataGridViewTextBoxColumn
       Create_Column_TextBox = New DataGridViewTextBoxColumn
       Create_Column_Common(Create_Column_TextBox, Name, Header,
Read_Only)
End Function

Public Function Create_Column_Checkbox(ByVal Name As String, ByVal
Header As String, Optional ByVal Read_Only As Boolean = False) As
DataGridViewCheckBoxColumn
       Create_Column_Checkbox = New DataGridViewCheckBoxColumn
       Create_Column_Checkbox.FalseValue = "Yes"
       Create_Column_Checkbox.TrueValue = "No"
       Create_Column_Common(Create_Column_Checkbox, Name, Header,
Read_Only)
End Function

Then to actually add the columns:

' This must be before .Fill() to stop the DGV from generated its own
column list.
<datagridview>.AutoGenerateColumns = False

<datagridview>.Columns.AddRange _
( _
 New DataGridViewColumn() _
 { _
  Create_Column_TextBox("Text_Column", "text"), _
  Create_Column_Checkbox("Checkbosx_Column", "yes/no", True) _
 } _
)

HTH,
B.
Guy Thornton - 24 Jul 2006 02:30 GMT
I was able to get your code suggestion to work.  However, it is not doing
what I was intending.  Remember that I have a Questionnaire Object that holds
a collection of questions.  Each question has an answer property.  I am
displaying the questions in the datagridview as column1 and the question
answer as column 2.  My problem is that I need column to to switch between
being a textbox column or a combobox column depending on the type of answer
the question requires.

The code segment posted adds 2 separate columns.  One a textbox column, the
other a boolean column.

If you have any further suggestions I am still searching for an answer to
this problem.

> > Brian,
> >
[quoted text clipped - 136 lines]
> HTH,
> B.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.