0

I have requirement where I have to show a tree view with list of tables and if user selects any table then it should show a grid with list of columns & rows should have a DataGridCheckboxColumns, and user can edit the checkboxcolumns.

Currently I am facing technical challenges in performing this.

I'm able to create columns but rows are not getting displayed and more over on change of Tree View selection though I have the Datatable updated but if I assign to grid it is not reflected in UI. Please help me on this its really an emergency requirement.

Thanks in Advance. Please find the code snippet below.

enter image description here

//DataGrid Add Columns and Rows
private void DataGridInitialize(string tableName)
{
    DataSet ds = selectionViewBAL.GetColumnNames(tableName);
    DataSet dataset = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("Functions");
    dt.Columns.Add("AllColumns");

    foreach (var row in ds.Tables[0].Rows)
    {
        string rowname = ((System.Data.DataRow)(row)).ItemArray[0].ToString();
        dt.Columns.Add(rowname);
    }

    int i = 0;
    foreach (DataColumn col in dt.Columns)
    {
        if (i == 0)
        {
            dataGrid1.Columns.Add(
              new DataGridTextColumn
              {
                  Header = "Functions",
                  Binding = new Binding(string.Format("[{0}]", "Functions"))
              });
        }
        else
        {
            dataGrid1.Columns.Add(
              new DataGridCheckBoxColumn
              {
                  Header = col.ColumnName,
                  Binding = new Binding(string.Format("[{0}]", col.ColumnName))
              });
        }
        i++;
    }

    DataSet fetchFunctionNames = new DataSet();
    fetchFunctionNames = selectionViewBAL.GetListOfFunctions(); //FetchFunctions();

    DataRow dr = dt.NewRow();

    var itemsArray = new object[dataGrid1.Columns.Count];
    for (int j = 0; j < fetchFunctionNames.Tables[0].Rows.Count; j++)
    {
        itemsArray[0] = fetchFunctionNames.Tables[0].Rows[j].ItemArray[1];
        for (i = 1; i < itemsArray.Length; i++)
        {
            itemsArray[i] = true;
        }

        dr.ItemArray = itemsArray;

        dt.Rows.Add(dr);
        dr = dt.NewRow();
    }

    dataGrid1.DataContext = dt;
}

I'm able to see everything in dt, but I'm not able to assign the same to datagrid and I cant edit though.

<DataGrid Grid.Row="0" Grid.ColumnSpan="4" Grid.Column="1"
          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
          HorizontalAlignment="Left" VerticalAlignment="Top" Margin="7,9,0,0"
          Name="dataGrid1" ItemsSource="{Binding}">
LPL
  • 16,827
  • 6
  • 51
  • 95
user1095239
  • 183
  • 1
  • 3
  • 8
  • I know the issue mainly lies with binding, please help me on this, the output looks like the image attached but help me in binding it properly. – user1095239 Feb 24 '13 at 18:55

1 Answers1

0

Bind to the DataTable.DefaultView Property.

dataGrid1.ItemsSource = dt.DefaultView;

Please see this WPF Snippet Tutorial - Binding a DataTable to a DataGrid too.

LPL
  • 16,827
  • 6
  • 51
  • 95
  • Thanks for your response, but some how it didnot work in my case. I have created datagrid with set of columns and assigning datatable to datagrid is displaying double set of columns and values in 1st set shows checkboxcolumn and second set shows bool values (True/False) as text. If I select different table name in Tree it is updated in Datatable and I can see updated column names in debug mode but on UI it shows the first set values. How should I proceed further – user1095239 Feb 25 '13 at 05:17