Click here to Skip to main content
15,672,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ALl,

I am trying to bind the data to datagrid in c#, Datagrid is created in xaml file and C# code binding the data, while debugging the result is proper but after binding its showing horizontal lines only

What I have tried:

//Datagrid - xaml.file
XAML
<DataGrid Name="dataGrid1" Margin="55,94,55,73"  AutoGenerateColumns="False">
</DataGrid>

/// xmal.cs file
C#
private async void selected_RoiChange(object sender, System.EventArgs e)
{            
    List<roi> roiNumber = new List<roi>();                     
    await QueuedTask.Run(() => FillDataGrid());
}

private void FillDataGrid()
{
    var gdbPath = @"C:\Users\Admin\Documents\ArcGIS\Projects\CIM_Project\CIM_Project.gdb";
    //Row row;
    var dataGridSource = new DataTable();
    using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(gdbPath))))
    using (Table table = fileGeodatabase.OpenDataset("CIM_ROI_Table"))
    {
        var data = new ObservableCollection<dynamicdatarow>();
        //var dataGridSource = new DataTable();
        List<string> dtr = new List<string>();
        ObservableCollection<keyvaluepair<string, string="">> AttributeList = new ObservableCollection<keyvaluepair<string, string="">>() { };
        QueryFilter queryFilter = new QueryFilter { WhereClause = "ROI_Number = 'CE12021P27301'" };
        using (Selection selection = table.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal))
        {
            IReadOnlyList<long> readOnlyList = selection.GetObjectIDs();
            using (RowCursor rc = table.Search())
            {
                RowCursor cursor = table.Search();
                IReadOnlyList<field> flds = cursor.GetFields();
                foreach (var fld in flds)
                {
                    if (fld.Name != "OBJECTID" && fld.Name != "Job_Status" && fld.Name != "Project_Name" && fld.Name != "Senior_Officer_Name" && fld.Name != "Contractor_Name" && fld.Name != "Voltage")
                    {
                        //Headers.Add(new ColumnData()
                        //{
                        //    Data = fld.Name,
                        //    Width = 100
                        //});
                        AddColumn<double>(dataGridSource, fld.Name.ToString());

                    }

                }
                var doubleGenerator = new Random();
                for (int rowIndex = 0; rowIndex < flds.Count; rowIndex++)
                {
                    var columnValues = new List<object>();
                    columnValues.Add(rowIndex < 1 ? "x" : "y");
                    for (int columnIndex = 0; columnIndex < dataGridSource.Columns.Count; columnIndex++)
                    {
                        columnValues.Add(doubleGenerator.NextDouble());
                    }
                    AddRow(dataGridSource, columnValues);
                }

            }
            Dispatcher.Invoke(
                new ThreadStart(() =>dataGrid1.ItemsSource = dataGridSource.DefaultView));

        }
    }

}

//And supported methods

private void AddColumn<tdata>(DataTable dataTable, string columnName, int columnIndex = -1)
{
    DataColumn newColumn = new DataColumn(columnName, typeof(TData));

    dataTable.Columns.Add(newColumn);
    if (columnIndex > -1)
    {
        newColumn.SetOrdinal(columnIndex);
    }

    int newColumnIndex = dataTable.Columns.IndexOf(newColumn);

    // Initialize existing rows with default value for the new column
    foreach (DataRow row in dataTable.Rows)
    {
        row[newColumnIndex] = default(TData);
    }
}

private void AddRow(DataTable dataTable, IList<object> columnValues)
{
    DataRow rowModelWithCurrentColumns = dataTable.NewRow();
    dataTable.Rows.Add(rowModelWithCurrentColumns);
}
Posted
Updated 22-Nov-22 11:00am
v2

Quote:
C#
private void AddRow(DataTable dataTable, IList<object> columnValues)
{
    DataRow rowModelWithCurrentColumns = dataTable.NewRow();
    dataTable.Rows.Add(rowModelWithCurrentColumns);
}
Your code ignores the columnValues parameter, and instead adds a new completely blank row.

You either need to update the rowModelWithCurrentColumns with the values from the columnValues list, or convert the list to an array and call the DataRowCollection.Add[^] overload which accepts the values for the new row.
C#
private void AddRow(DataTable dataTable, IList<object> columnValues)
{
    dataTable.Rows.Add(columnValues.ToArray());
}
 
Share this answer
 
Comments
Member 10960846 23-Nov-22 4:21am    
Hi Richard, Thanks for the help, I have tried as same, but not working, it's showing as Horizontal lines in the grid.
Richard Deeming 23-Nov-22 4:24am    
Probably because you have AutoGenerateColumns="False" but you haven't defined any columns.
With WPF & DataGrid control you should be using Data Binding (ref: Microsoft Learn)[^].

When using Data Binding, You work directly on the data, not the DataGrid itself. The DataGrid is then a "view of the data", therefore any changes made to the data are automatically reflected in the view - the Data Binding does all of the work for you.

Here is a tutorial on DataBinding: Introduction to WPF data binding - The complete WPF tutorial[^]

And here is a tutorial on the DataGrid control: The DataGrid control - The complete WPF tutorial[^]

Hope this helps!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900