is there a way to programmatically create data binding to DataGrid, so that in such a binding method, i receive the item in each row, and access data in the item arbitrarily.
for example,
class Addition
{
public int P1 {get;set;}
public int P2 {get;set;}
}
in my DataGrid, i would like to have an other column, output the sum of them, in this example.
in my real problem, this gets more severe and data comes dynamically, i can't even foresee the numbers and titles of columns(real data represent a row as Dictionary).
data could be considered as readonly.
Update
maybe a better example about what i want to achieve will help you and me better.
i have an array of dictionary here.
Dictionary<string, int>[] data;
given two dictionaries in the same array, they must have the same key set.
so let say, in such array, the key set is {"P1", "P2"}
.
but another array comes,
Dictionary<string, int>[] data2;
in this array, the key set is {"P1", "P2", "P3"}
.
so i have to do here, is to have a DataGrid, adapting all key sets, and have another column, the value is sum-them-all.
This problem example shows the same essence as my real problem.
Actually I'm not sure if a data grid view may fit your situation, but it's much customizable and you can fully control it dynamically.
You can modify the class, and add a data member for the addition of P1 and P2, and then make the data source of you dataGridView is a list of this class
class Addition
{
public int P1 {get;set;}
public int P2 {get;set;}
public long varSum {get;set;}
public void Addition (int P1, int P2)
{
this.P1 = P1;
this.P2 = P2;
this.varSum = P1 + P2;
}
}
And then
Addition add1 = new Addition (1,2);
List <Addition> addList = new List <Addition> ();
addList.insert(add1);
// you need to define dgv as a DataGridVirw
dgv.dataSource = addList;
Then your data grid view will have three columns, P1, P2 and sum of them (varSum).
Well, the data grid data view source may be a list of string array, where each single DGV row is a string array (eg {"1", "2", "3"}). Then, you can convert your dictionary to a string array for each DGV row and then build a list of these arrays and add each row separately to your DGV.
int n = 10;/* retrieve this value from your dictionary as your columns count*/;
string [] xx = new string[n];
dataGridView1.ColumnCount = n;
for (int j = 0; j < n; j++)
{
xx[j]=("0"); // fill from your dictionary
dataGridView1.Columns[j].HeaderText = "someColHeaderText";
}
int m = 10;/* retrieve this value from your dictionary as your rows count*/;
for (int i = 0; i < m; i++)
{
dataGridView1.Rows.Add(xx);
dataGridView1.Rows[i].HeaderCell.Value = "someRowHeaderText";
}
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.