Is it possible to dynamically set DataGrid Column Styles

G

Guest

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
 
B

Brian Tkatch

Guy said:
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

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.
 
G

Guest

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?
 
B

Brian Tkatch

Guy said:
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?

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top