ListView转换为二维数组
/// <summary>
/// 将ListView的数据导出到二维数组。
/// </summary>
/// <param name="listView">二维数据视图</param>
/// <param name="includeColumnText">是否要把列标题文本也导到数组中。</param>
/// <remarks>
/// <作者>长江支流</作者>
/// <日期>2005-08-21</日期>
/// <修改></修改>
/// </remarks>
/// <returns>二维数组。</returns>
public string[,] ToStringArray(ListView listView,bool includeColumnText)
{
ListView lvw = listView;
int rowsCount = lvw.Items.Count;
int colsCount = lvw.Columns.Count;
//包括列标题
if (includeColumnText)
{
rowsCount++;
}
string[,] arrReturn = null;
arrReturn = new string[rowsCount,colsCount];
int i = 0;
if (includeColumnText)
{
//写标题
for(i = 0 ; i < colsCount; i++)
{
arrReturn[0,i] = lvw.Columns[i].Text;
}
i = 1;
}
//写数据行Items
int rowIndex = 0;
for(; i < rowsCount; i++,rowIndex++)
{
for (int j = 0; j < colsCount; j++)
{
arrReturn[i,j] = lvw.Items[rowIndex].SubItems[j].Text;
}
}
return arrReturn;
}
打印ListView:
private void btnPrintEasy_Click(object sender, System.EventArgs e)
{
MisGoldPrinter webmis = new MisGoldPrinter(); //打印组件
webmis.Title = "MIS金质打印通/nWWW.WebMIS.COM.CN"; //网格标题
webmis.DataSource = ToStringArray(listView,true); //任意二维的数据通通打印
webmis.Preview(); //打印预览
}
VB.Net:
Private Sub btnPrintEasy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintEasy.Click
Dim webmis As MisGoldPrinter '打印组件
webmis = New MisGoldPrinter
webmis.Title = "MIS金质打印通"+vbCrLf+"WWW.WebMIS.COM.CN" '网格标题
webmis.DataSource = ToStringArray(listView,true) '任意二维的数据通通打印
webmis.Preview() '打印预览
End Sub