0

I'm trying to add 3 columns of checkboxes in my data grid.

First, with method I put some string values in two columns and after that I want to add 3 more columns of checkboxes.

I found this solution in other post like this one:

DataGridCheckBoxColumn chbcolumn = new DataGridCheckBoxColumn();
for (int j = 0; j == 3; j++)
{
     tabela.Columns.Add(chbcolumn);
}

(tabela is the name of the data grid defined in XAML like this):

<DataGrid AutoGenerateColumns="True" Height="206" HorizontalAlignment="Left" Margin="12,265,0,0" Name="tabela" VerticalAlignment="Top" Width="556" SelectionChanged="tabela_SelectionChanged" Grid.RowSpan="2" />

Now this works good for adding one column, but I need two more. I've tried putting that code in for loop, but then I don't get nothing, so obliviously that was a stupid idea. I've tried some other properties of Columns too, but didn't manege to find one that works in this case.

Also, do you maybe know a way to access the name of Columns and changing them? Cause I need 3 different names for those columns.

Does anyone maybe knows some easy way to solve this?

Update:

I make it this way, maybe not the perfect solution, but it did a job.

DataGridCheckBoxColumn chbcolumn1 = new DataGridCheckBoxColumn();
DataGridCheckBoxColumn chbcolumn2 = new DataGridCheckBoxColumn();
DataGridCheckBoxColumn chbcolumn3 = new DataGridCheckBoxColumn();
chbcolumn1.Header = "Controller";
chbcolumn2.Header = "Area";
chbcolumn3.Header = "Service";
tabela.Columns.Add(chbcolumn1);
tabela.Columns.Add(chbcolumn2);
tabela.Columns.Add(chbcolumn3); 
4
  • They should be added when you adding them in loop. Commented Feb 5, 2014 at 10:53
  • @ethicallogics hmmm maybe I'm doing it wrong way. I've change the post, adding for loop like I did the first time. Do you maybe see something that's not well defined? Commented Feb 5, 2014 at 11:02
  • 1
    look at stackoverflow.com/questions/1754608/… Commented Feb 5, 2014 at 11:10
  • Check this... stackoverflow.com/a/5224523/3156647 Commented Feb 5, 2014 at 11:35

2 Answers 2

5

Instead of using code behind, try it with XAML. A simple checkbox column can be defined like this

    <DataGrid ItemsSource="{Binding MyDataList}" AutoGenerateColumns="False">
      <DataGrid.Columns>            
       <DataGridTextColumn Header="TextColumn1" Binding="{Binding FirstName}" />
       <DataGridTextColumn Header="TextColumn1" Binding="{Binding LastName}" />
       <DataGridTextColumn Header="TextColumn1" Binding="{Binding Address}" />

       <DataGridTemplateColumn Header="CheckBoxColumn1">
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <CheckBox IsChecked="{Binding IsActive}"/>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>

       <DataGridTemplateColumn Header="CheckBoxColumn2">
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <CheckBox IsChecked="{Binding IsAlive}"/>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>

       <DataGridTemplateColumn Header="CheckBoxColumn3">
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <CheckBox IsChecked="{Binding IsParticipating}"/>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>  
Sign up to request clarification or add additional context in comments.

Comments

0

You are adding the same instance Column in a loop. Try this

for (int j = 0; j == 3; j++)
{
 DataGridCheckBoxColumn chbcolumn = new DataGridCheckBoxColumn();
 tabela.Columns.Add(chbcolumn);
 }

1 Comment

I've tried that way the first time, but it was the same. Anyway I've managed to fix it in the other way. I made 3 different instance of DataGridCheckBoxColumn... It's not maybe the best solution, but it will work for now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.