listview数据显示问题
在程序中用listview显示数据,但查询回来的数据中有的能够显示, 有的只能显示出第一列,并且从文件中看,数据是都能读出来的, 不知是什么原因,有人能帮着看看吗?
private void button1_Click(object sender, EventArgs e)
{
string ID =this.ID_textBox.Text.Trim();
string factroy = this.Factory_textBox.Text.Trim();
string Time = this.Time_textBox.Text.Trim();
string kind = this.Kind_textBox.Text.Trim();
string name =this.bookname_textBox.Text.Trim();
string ID_SQLcommand=null;
string Factroy_SQLcommand = null;
string Time_SQLcommand = null;
string Kind_SQLcommand = null;
string Name_SQLcommand = null;
bool first_sql = true; // 是否为首个查询条件
//string Select_sqlcommand = "select * from ToolsBook ";
string Select_sqlcommand = "select * from Book_info ";
if (ID.Length == 0)
{
ID_SQLcommand = "";
}
else
{
if (first_sql == true)
{
ID_SQLcommand = "where 图书编号 ='" + @ID + "'";
first_sql = false;
}
else
{
ID_SQLcommand = "and 图书编号 ='" + @ID + "'";
}
}
if (factroy.Length == 0)
{
Factroy_SQLcommand = "";
}
else
{
if (first_sql == true)
{
Factroy_SQLcommand = "where 厂家名称 like'%" + @factroy + "%'";
first_sql = false;
}
else
{
Factroy_SQLcommand = "and 厂家名称 like'%" + @factroy + "%'";
}
}
if (Time.Length ==0)
{
Time_SQLcommand = "";
}
else
{
if(first_sql == true)
{
Time_SQLcommand = "where 出版日期 ='" + @Time + "'";
first_sql = false;
}
else
{
Time_SQLcommand = "and 出版日期 ='" + @Time + "'";
}
}
if (kind.Length == 0)
{
Kind_SQLcommand = "";
}
else
{
if (first_sql == true)
{
Kind_SQLcommand = "where 元器件类型 like'%" + @kind + "%'";
first_sql = false;
}
else
{
Kind_SQLcommand = "and 元器件类型 like'%" + @kind + "%'";
}
}
if( name.Length==0)
{
Name_SQLcommand = "";
}
else
{
if (first_sql == true)
{
Name_SQLcommand = "where 手册名称 like'%" + @name + "%'";
first_sql = false;
}
else
{
Name_SQLcommand = "and 手册名称 like'%" + @kind + "%'";
}
}
//string Select_sqlcommand = "select * from ToolsBook where book_id ='" + @ID + "'";
// string Select_sqlcommand = "select * from ToolsBook where factory ='" + @factroy + "'and book_time ='" + @Time + "'";
Select_sqlcommand += ID_SQLcommand + Factroy_SQLcommand + Time_SQLcommand + Kind_SQLcommand;
if (this.DataBase1.con.State == ConnectionState.Closed)
{
this.DataBase1.con.Open();
}
this.DataBase1.mydadp.SelectCommand = new SqlCommand(Select_sqlcommand);
try
{
this.DataBase1.mydadp.SelectCommand.Connection = this.DataBase1.con;
if (this.DataBase1.mydset.Tables.Contains("selected_book_info") == true)// 若之前已经别的查询结果 则将其清空
{
Clear_Num++;
this.DataBase1.mydset.Tables["selected_book_info"].Clear();
}
this.DataBase1.mydadp.Fill(this.DataBase1.mydset, "selected_book_info");// 将查询结果集合填入Dataset中并将dataTable命名为"selected_book_info"
this.listView1.BeginUpdate();
//listView1_LoadData();
listView1_RELoadData();
this.listView1.EndUpdate();
MessageBox.Show("Clear 调用了" + Clear_Num+"次");
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
private void listView1_RELoadData()
{
FileStream fs1 = new FileStream("F:\\test1.txt", FileMode.Create);
FileStream fs2 = new FileStream("F:\\test2.txt", FileMode.Create);
StreamWriter sw1 = new StreamWriter(fs1);
StreamWriter sw2 = new StreamWriter(fs2);
//string[,] temp = new string[1000, 9];
//this.listView1.Items.Clear();
this.listView1.Clear();// 移除listview中所有项和列
DataTable dt2 = this.DataBase1.mydset.Tables["selected_book_info"];//获取dataset中的数据表all_book_info
DataRow dr = null;
int rowcount = dt2.Rows.Count;
int colcount = dt2.Columns.Count;
if (rowcount == 0)
{
MessageBox.Show("未查到相关信息");
}
else
{
for (int i = 0; i < colcount; i++)
{
this.listView1.Columns.Add(dt2.Columns[i].Caption.Trim(), this.listView1.Width /(colcount+1), HorizontalAlignment.Center);// 增加列
}
for (int j = 0; j < rowcount; j++)
{
dr = dt2.Rows[j];// 读出来一行数据
sw2.Write(dr[0].ToString().Trim() + "end");
this.listView1.Items.Add(dr[0].ToString().Trim());// 由每行第一列数据造出数据表
for (int k = 1; k < colcount; k++)
{
string info = dr[k].ToString().Trim();
sw2.Write( info + "end");
this.listView1.Items[j].SubItems.Add(info);
}
sw2.Write("\n");
}
sw2.Write("数据共有" + rowcount + "条end");
}
mylistviewform.Num++;
sw2.Write("Num为" + Num + "end");
//清空缓冲区
sw1.Flush();
sw2.Flush();
//关闭流
sw1.Close();
sw2.Close();
fs1.Close();
fs2.Close();
}