Windows Forms: How to display multiple values in a column
I have a situation where in I have to开发者_如何学C display data in tabular form with each cell having multiple pair of values,list of values as show below
Column1 Column2 Column3
1 v1 v2 vl1
vl2
vl3
2 v3 v4 vl4
vl5
vl6
How can I do this using windows forms.
Any help is greatly appreciated.
Thanks JeeZ
Have you tried setting the wrapmode for the column to allow for '\n'? Here's a quick example that seems to yield the format you posted above.
dataGridView1.Columns.Add("Column1", "Column1");
dataGridView1.Columns.Add("Column2", "Column2");
dataGridView1.Columns.Add("Column3", "Column3");
//this is the key line...to allow \n in column
dataGridView1.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Rows.Add(new object[] { 1, "v1 v2", "vl1\nvl2\nvl3" });
dataGridView1.Rows.Add(new object[] { 2, "v3 v4", "vl4\nvl5\nvl6" });
If you are using a DataGridView
, a solution would be to format those values into a single string, and display that string in the appropriate column of the grid.
A way of doing this would be to handle the CellFormatting
event of the grid, and for that column put the formatted value into the e.Value
property of the event arguments.
'if the grid is bound to a DataSet, we have a DataRowView as the DataBoundItem
var row = this.logicalGridView.Rows[e.RowIndex].DataBoundItem as DataRowView;
if (e.ColumnIndex == 2)
{
e.Value = String.Format("{0} {1}", row["Column1"], row["Column2"]);
e.FormattingApplied = true;
}
(An other solution would be to create a custom column type for the grid, and paint the cells yourself. Of course, this would be more complicated to do.)
This may be an indication that you'd be better off using third-party controls.
The System.Windows.Forms controls are extremely limited. I don't know whether this particular problem is solvable with the DataGridView, but if you have an application of reasonable complexity (say, 15 to 20 screens), you will probably save quite a bit of time and effort purchasing third-party controls.
I can highly recommend Developer Express from personal experience: their controls have very extensive functionality, are reasonably prices, and their support people know the product up and down.
A ListView Control would work as well.
listView1.Items.Clear();
listView1.View = View.Details;
ColumnHeader ch1 = listView1.Columns.Add("Index");
ColumnHeader ch2 = listView1.Columns.Add("Column1");
ColumnHeader ch3 = listView1.Columns.Add("Column2");
ColumnHeader ch4 = listView1.Columns.Add("Column3");
ListViewItem lvi1 = listView1.Items.Add("1");
lvi1.SubItems.Add("1");
lvi1.SubItems.Add("v1 v2");
lvi1.SubItems.Add("vl1");
ListViewItem lvi2 = listView1.Items.Add("2");
lvi2.SubItems.Add("");
lvi2.SubItems.Add("");
lvi2.SubItems.Add("vl2");
ListViewItem lvi3 = listView1.Items.Add("3");
lvi3.SubItems.Add("");
lvi3.SubItems.Add("");
lvi3.SubItems.Add("vl3");
ListViewItem lvi4 = listView1.Items.Add("4");
lvi4.SubItems.Add("2");
lvi4.SubItems.Add("v3 v4");
lvi4.SubItems.Add("vl4");
ListViewItem lvi5 = listView1.Items.Add("5");
lvi5.SubItems.Add("");
lvi5.SubItems.Add("");
lvi5.SubItems.Add("vl5");
ListViewItem lvi6 = listView1.Items.Add("6");
lvi6.SubItems.Add("");
lvi6.SubItems.Add("");
lvi6.SubItems.Add("vl6");
精彩评论