Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:46:53+00:00 2026-06-04T17:46:53+00:00

I am working with DataGrids but I am struggling to binding my data since

  • 0

I am working with DataGrids but I am struggling to binding my data since the number of columns varies depending of the info that has to be showed.

So, what I have tried to do is to create and object which contains all the columns and rows that I need at some point and binding this object to the ItemsSource property. Since I have worked with DataGridViews in WindowsForms I have in mind something like this:

DataTable myTable = new DataTable();

DataColumn col01 = new DataColumn("col 01");
myTable.Columns.Add(col01);
DataColumn col02 = new DataColumn("col 02");
myTable.Columns.Add(col02);

DataRow row = myTable.NewRow();
row[0] = "data01";
row[1] = "data02";
myTable.Rows.Add(row);

row = myTable.NewRow();
row[0] = "data01";
row[1] = "data02";
myTable.Rows.Add(row);

But I haven’t been able to find a way to do the same thing in WPF since I need some columns to be DataGridComboBoxColumns for example.

Actually I have read many post about it in this site, but none of them helped to me. I am really lost.

Could anyone help me? I just need to be able to create a table which may contain DataGridTextColumns or `DataGridComboBoxColumns, etc, In order to bind this final object to the DataGrid’s ItemsSource property.

Hope someone can help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:46:55+00:00Added an answer on June 4, 2026 at 5:46 pm

    Okay, let me try to take an example which is similar to your needs

    Let’s assume we use this class:

    public class MyObject
    {
       public int MyID;
       public string MyString;
       public ICommand MyCommand;
    }
    

    And we are willing to display a DataGrid listing the ID, and having as a second column a Button, with the property MyString as content, which, when clicked, launches the ICommand MyCommand which opens in a new window whatever you want.

    Here is what you should have on the View side:

        <DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding MyID}" />
                <DataGridTemplateColumn Header="Buttons">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding MyString}" Command="{Binding MyCommand}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    This will show a DataGrid taking all the content in an IEnumerable<MyObject> named ‘MyList’, and shows two columns as defined before.

    Now if you need to define the command.
    First, I recommend you read this introductory link to MVVM and take the RelayCommand class (that’s what we’re gonna use for your problem)

    So, in your ViewModel, the one which defines the MyList, here is how you should define some of the useful objects:

    public ObservableCollection<MyObject> MyList { get; set; }
    
    // blah blah blah
    
    public void InitializeMyList()
    {
      MyList = new ObservableCollection<MyObject>();
      for (int i = 0; i < 5; i++)
      {
        MyList.Add(InitializeMyObject(i));
      }
    }
    
    public MyObject InitializeMyObject(int i)
    {
      MyObject theObject = new MyObject();
      theObject.MyID = i;
      theObject.MyString = "The object " + i;
      theObject.MyCommand = new RelayCommand(param =< this.ShowWindow(i));
      return theObject
    }
    
    private void ShowWindow(int i)
    {
      // Just as an exammple, here I just show a MessageBox
      MessageBox.Show("You clicked on object " + i + "!!!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code working with a DataGrid that has two column Column_A
I've List and i bind these list to datagrid that is working fine, but
I'm working on creating a datagrid that has a checkbox column. I have some
I am working on WPF and DataGrids, I am really new on this. And
Im working at a DataGrid which has a custom itemRenderer with a checkbox and
I'm recently working on a WPF application that uses the datagrid from the WPF
I'm working on creating a custom winforms datagridview control that inherits the built in
I already had this stuff working in another project but now it does not.
I am working in a Silverlight 4 application. I have a datagrid that I
I have 2 datagrids. Both with checkboxes. I have a nested foreach loop that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.