I am building a program that loads an xml to a datagridview,
But I need to add 2 more columns one with buttons and one with images.
How can I add them here: ?
DataSet data = new DataSet(); string p = System.IO.Path.Combine(Application.StartupPath, "payday.xml");
data.ReadXml(p);
this.dataGrid.DataSource = data;
this.dataGrid.DataMember = "costumer";
int i = 0;
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
if (column.Name == "Name" || column.Name == "Status" || column.Name == "URL" || column.Name == "type" || column.Name == "Last-Checked-Pay")
{
column.Visible = true;
column.Width = (int)(dataGrid.Width * .2) + (column.Name.Length / 2);
}
else
{
//I tried to do it here:
//dataGrid.Columns[i+1].CellType = new DataGridViewButtonColumn();
//dataGrid.Columns[i+1].HeaderCell.
}
开发者_开发百科 i++;
}
This will help with adding to the column collection (although you can shorthand the add by implicitly adding to the collection using Add on the DataGridView). As far as adding data goes, you will likely want to add using the row binding event so you can populate those columns.
The other option is add the information needed to the dataset and then bind the newly added columns (fields).
The first option dinks with the DataGridView programatically, while the second allows you to add a declarative set up for the DataGridView and massages the data that is bound.
Which to choose? If the DataGridView always has these fields, I would massage the data. If the added columns only appear some of the time, I would alter the DataGridView programmatically.
精彩评论