I have built a datagrid using datatable and the results are displayed correctly. Now I intend to add ROWHEADERs to the datagrid. I populated an observable collection<string>
and in the XAML
, I use that as row header data template path. Still, it is not showing up. Can somebody advice where the issue is?
public ObservableCollection<string> Rownameheaders { get; set; } //in the main .cs
//xaml.cs
public void Form1_load(object sender, EventArgs e)
{
WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
var hello1 = new ObservableCollection<String>();
for(int i=0; i< model.prodwells.Count;i++)
{
hello1.Add(model.prodwells[i]);
}
model.Rownameheaders = hello1;
List<double[]> list = new List<double[]>();
for (int i = 0; i < model.RowFijtable; i++)
{
double[] rowdata = new double[model.ColFijtable];
for (int j = 0; j < model.ColFijtable; j++)
{
rowdata[j] = model.TauTable[i, j];
}
list.Add(rowdata);
}
DataTable table = ConvertListToDataTable(list);
dataGridView1.ItemsSource = table.AsDataView();
}
private DataTable ConvertListToDataTable(List<double[]> list)
{
DataTable table = new DataTable();
WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
// Provide default column name & data type
table.Columns.Add(model.injwells[i], typeof(double));
}
// Add rows.
foreach (var array in list)
{
// assign each array element to the appropriate column
var row = table.NewRow();
for (int i = 0; i < array.Length; ++i)
row.SetField(i, array[i]);
table.Rows.Add(row);
}
return table;
}
XAML:
<DataGrid Name="dataGridView1" Loaded="Form1_load" ItemsSource="{Binding}" AutoGenerateColumns="True" AlternatingRowBackground="Gainsboro" CanUserSortColumns="False">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Rownameheaders}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
This is the result I get but with no row headers...
I ended up adding a new column with values and setting the column location to 0 in the datatable. Below is the updated code:
private DataTable ConvertListToDataTable(List<double[]> list)
{
DataTable table = new DataTable();
WF_CRM_RPluginModel model = DataContext as WF_CRM_RPluginModel;
// Get max columns.
int columns = 0;
foreach (var array in list)
{
if (array.Length > columns)
{
columns = array.Length;
}
}
// Add columns.
for (int i = 0; i < columns; i++)
{
// Provide default column name & data type
table.Columns.Add(model.injwells[i], typeof(double));
}
// Add rows.
foreach (var array in list)
{
// assign each array element to the appropriate column
var row = table.NewRow();
for (int i = 0; i < array.Length; ++i)
row.SetField(i, array[i]);
table.Rows.Add(row);
}
//ADDED below part
DataColumn newCol = new DataColumn("Prod Name", typeof(string));
newCol.AllowDBNull = true;
table.Columns.Add(newCol);
newCol.SetOrdinal(0);
int m = 0;
foreach(DataRow row in table.Rows)
{
row["Prod Name"] = model.prodwells[m];
m = m + 1;
}
return table;
}
hope it helps others too!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.