C# ListBox 中的多列数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11876983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-column data in ListBox
提问by sara brown
I want to have multi-column in my ListBox. Below is the example of picture I got in my application.
我想在我的列表框中有多列。下面是我在我的应用程序中得到的图片示例。


I actually have about 7 columns, but printed out only two columns to make it easier to understand.
我实际上有大约 7 列,但只打印了两列以使其更容易理解。
So, the first column would say dateand the second column would say name. As you can see, the data did not go into their own columns.
所以,第一列会说date,第二列会说name。如您所见,数据没有进入它们自己的列。
This is my code:
这是我的代码:
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
foreach (XmlNode xn in xnList)
{
string date = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Date").FirstChild.Value;
string id = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "ID").FirstChild.Value;
if (date == cari)
{
this.listBox1.Items.AddRange(new object[] {
//dateBox.Text,
dateBox.Text + "\r\n" + date});
this.listBox1.Items.AddRange(new object[] {
"sarabrown"});
}
}
this.listBox1.Location = new System.Drawing.Point(12, 28);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(300, 95);
this.listBox1.TabIndex = 0;
this.listBox1.ColumnWidth = 100;
//
// Form3
//
this.ClientSize = new System.Drawing.Size(400, 273);
this.Controls.Add(this.listBox1);
this.Name = "Form3";
this.ResumeLayout(false);
I found this code there, but it creates a list box that looks just like the one pictured above. Is there anyone knows about this?
我在那里找到了这段代码,但它创建了一个与上图一样的列表框。有没有人知道这个?
回答by Kash
The MultiColumnproperty of ListBoxonly helps to avoid vertical scrolling hence just stacks the overflowing items into the next column. The requirement you have is not available by default in .NET. Hence you may have to build your own custom control to support that.
only的MultiColumn属性ListBox有助于避免垂直滚动,因此只会将溢出的项目堆叠到下一列中。默认情况下,您的要求在 .NET 中不可用。因此,您可能必须构建自己的自定义控件来支持它。
Btw, GridViewis your friend.. What you need is easily achievable using GridView.
For e.g., to make it simplistic (you may have to tweak this entirely to suit your problem)
顺便说一句,GridView是你的朋友..你需要的很容易使用GridView. 例如,使其简单化(您可能需要完全调整以适应您的问题)
protected void MyGridView_PreRender(object sender, EventArgs e)
{
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(new StringReader(myXmlDoc.OuterXml));
GridView gv = (GridView)sender;
gv.DataSource = myDataSet;
gv.DataBind();
}
UPDATE:
更新:
You may want to check out ListViewinstead of GridViewor ListBox. It is comparatively lightweight than a GridView.
您可能想签出ListView而不是GridView或ListBox。它比GridView.
With ListViewyou can also put in other controls in the different columns like checkboxes.
随着ListView你也可以把其他控件像复选框不同列。
Check this exampleout to give you an idea.
Or this one which is simpler: Using ListView control in C#.
看看这个例子给你一个想法。
或者这个更简单:在 C# 中使用 ListView 控件。
回答by Aman Dhally
For MutliColumn in ListBox in C#, this is working fine
对于 C# 中 ListBox 中的 MutliColumn,这工作正常
listBox1.Items.AddRange(
new object[]
{
"Name","Aman"
}
);
回答by Martin Verjans
I think you misunderstood the usage of MultiColumn property in ListBox.
我认为您误解了 ListBox 中 MultiColumn 属性的用法。
A multicolumn ListBox places items into as many columns as are needed to make vertical scrolling unnecessary.
多列 ListBox 将项目放入所需的尽可能多的列中,从而无需垂直滚动。
So this is just a thing to avoid scrolling.
所以这只是为了避免滚动。
If you want to display several columns with separate data on each column, I suggest you use ListView.
如果您想在每列上显示多个带有单独数据的列,我建议您使用ListView。
回答by PROTOS tecnologia
I have been able to create a code so that I can order dates from any year range from a spreadsheet in Excel.
我已经能够创建一个代码,以便我可以从 Excel 电子表格中订购任何年份范围内的日期。
Private Sub UserForm_Initialize()
With Me
.StartUpPosition = 0
'.Width = Application.Width * 0.46
'.Height = Application.Height * 0.57
.Left = Application.Left + (Application.Width * 0.7) \ 2
.Top = Application.Top + (Application.Height * 0.3) \ 2
End With
End Sub
Private Sub CommandButton1_Click()
initialize ("lth") 'Call rutine "initialize" to order listbox from down to up
End Sub
Private Sub CommandButton2_Click()
initialize ("htl") 'Call rutine "initialize" to order listbox from up to down
End Sub
Sub initialize(ByVal ordertype As String)
Dim x As Integer
Dim i As Integer
Dim fechas As Date
Dim datofecha As String
Dim arrayyear() As Date
Dim diasmesbisiesto(1 To 12) As Integer
Dim diasmesnobisiesto(1 To 12) As Integer
Dim lastrow As Integer
Dim mayoryear As Integer
Dim menoryear As Integer
Dim f As Integer
Dim c As Date
lastrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
ReDim arrayyear(lastrow)
For u = 1 To lastrow
arrayyear(u) = Format(Sheet1.Cells(u, 1).Value, "dd/mm/yyyy")
Next u
'-------------------------
'Obtener mayor valor fecha
'-------------------------
Dim vm As Integer
Dim maxindex As Integer
maxindex = 1
For vm = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(vm) > arrayyear(maxindex) Then
maxindex = vm
End If
Next
mayor = arrayyear(maxindex)
'-------------------------
'-------------------------
'Obtener menor valor fecha
'-------------------------
Dim vn As Integer
Dim minindex As Integer
minindex = 1
For vn = 2 To UBound(arrayyear) 'suponemos indice desde 1 a n
If arrayyear(minindex) < arrayyear(vn) Then
Else
minindex = vn
End If
Next
menor = arrayyear(minindex)
'-------------------------
menoryear = Year(menor) ' Asign the lowest year in menoryear
mayoryear = Year(mayor) ' Asign the highest year in mayoryear
ListBox1.Clear
'Carga vector diasmesbisiesto
'---------------------------
diasmesbisiesto(1) = 31
diasmesbisiesto(2) = 29
diasmesbisiesto(3) = 31
diasmesbisiesto(4) = 30
diasmesbisiesto(5) = 31
diasmesbisiesto(6) = 30
diasmesbisiesto(7) = 31
diasmesbisiesto(8) = 31
diasmesbisiesto(9) = 30
diasmesbisiesto(10) = 31
diasmesbisiesto(11) = 30
diasmesbisiesto(12) = 31
'---------------------------
'Carga vector diasmesnobisiesto
'---------------------------
diasmesnobisiesto(1) = 31
diasmesnobisiesto(2) = 28
diasmesnobisiesto(3) = 31
diasmesnobisiesto(4) = 30
diasmesnobisiesto(5) = 31
diasmesnobisiesto(6) = 30
diasmesnobisiesto(7) = 31
diasmesnobisiesto(8) = 31
diasmesnobisiesto(9) = 30
diasmesnobisiesto(10) = 31
diasmesnobisiesto(11) = 30
diasmesnobisiesto(12) = 31
'---------------------------
f = 0 'Variable para ubicar la ubicacion de la fila en donde se guardaran los datos en el listbox
cuenta = 0 ' Variable para contar la cantidad de elementos que se cargaron en el listbox
If ordertype = "lth" Then
GoTo 1 ' Ordering lowest to highest
End If
If ordertype = "htl" Then
GoTo 2 ' Ordering highest to lowest
End If
'---------------------------
' Ordering lowest to highest
'---------------------------
1:
For anio = menoryear To mayoryear
For mes = 1 To 12
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' A?os bisiestos
diasmes = diasmesbisiesto(mes)
Else
' A?os no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, 1), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c + x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Lowest to Highest "
Exit Sub
'---------------------------
' Ordering highest to lowest
'---------------------------
2:
For anio = mayoryear To menoryear Step -1
For mes = 12 To 1 Step -1
If (anio Mod 4 = 0 And anio Mod 100 <> 0 Or anio Mod 400 = 0) Then
' A?os bisiestos
diasmes = diasmesbisiesto(mes)
Else
' A?os no bisiestos
diasmes = diasmesnobisiesto(mes)
End If
datofecha = Format(DateSerial(anio, mes, diasmes), "dd/mm/yyyy")
fechas = datofecha
'Carga del listbox
'------------------------------------------------------------------------------------------------------------
For x = 0 To diasmes - 1
For i = 1 To lastrow
c = fechas
If Format(Sheet1.Cells(i, 1), "dd/mm/yyyy") = c - x Then
Me.ListBox1.AddItem
Me.ListBox1.List(f, 0) = Format(Sheet1.Cells(i, 1), "dd/m/yyyy")
cuenta = cuenta + 1
For b = 1 To 1
Me.ListBox1.List(f, b) = Sheet1.Cells(i, b + 1)
Next b
f = f + 1
End If
Next i
Next x
'------------------------------------------------------------------------------------------------------------
Next mes
Next anio
Label2.Caption = "Number of Elements: " & cuenta
Label3.Caption = "Order from Highest to Lowest "
Exit Sub
End Sub

