日期:2014-05-18 浏览次数:21250 次
void DataBind()
{
DataTable dt = InitTable();
List<string> lstSource = new List<string>();
lstSource.Clear();
DataTable dtSource = new DataTable();
if (dt != null && dt.Rows.Count > 0)
{
int page = 20; //假设超过20行则增加新列
int rem = dt.Rows.Count / page;
int remCount = rem;
if (dt.Rows.Count % page > 0)
{
remCount = remCount + 1;
}
int range = 0;
int index = 0;
foreach (DataRow row in dt.Rows)
{
if (range < page)
{
lstSource.Add(row["PERSON"].ToString() + "," + row["TELEPHONE"].ToString());
}
else
{
if (range % page != 0)
{
lstSource[index] = lstSource[index] + "," + row["PERSON"].ToString() + "," + row["TELEPHONE"].ToString();
index++;
}
else
{
if (index == page)
index = 0;
lstSource[index] = lstSource[index] + "," + row["PERSON"].ToString() + "," + row["TELEPHONE"].ToString();
index = 1;
}
}
range++;
}
}
if (lstSource.Count > 0)
{
string[] strColumns = lstSource[0].Split(new char[] { ',' });
for (int i = 0; i < strColumns.Length; i++)
{
dtSource.Columns.Add("Column" + i.ToString());
}
}
foreach (string str in lstSource)
{
string[] values = str.Split(new char[] { ',' });
dtSource.Rows.Add(values);
}
dataGridView1.DataSource = dtSource;
}
private DataTable InitTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("PERSON", typeof(string));
dt.Columns.Add("TELEPHONE", typeof(string));
for (int i = 0; i < 110; i++)
{
DataRow dr = dt.NewRow();
dr["PERSON"] = "联系人" + i.ToString();
dr["TELEPHONE"] = "联系电话" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}