listview数据显示问题

wzsb2010 2018-04-27 11:36:22
在程序中用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();
}
...全文
624 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoppo0505 2018-05-02
  • 打赏
  • 举报
回复
引用 4 楼 wzsb2010 的回复:
[quote=引用 2 楼 flashyyp123 的回复:] listView1.Items.Add 哪个循环加断点 调试
加断点看数据也没发现问题啊?[/quote] 那你需要提供更多的信息。 比如listview1的显示代码,dt2的数据等其他信息。
wzsb2010 2018-04-29
  • 打赏
  • 举报
回复
没人吗?
0 1看天下 2018-04-28
  • 打赏
  • 举报
回复
listView1.Items.Add 哪个循环加断点 调试
shoppo0505 2018-04-28
  • 打赏
  • 举报
回复
你贴的代码和你描述的问题关系不大。 个人感觉,可能返回的列名和显示的列名不匹配
wzsb2010 2018-04-28
  • 打赏
  • 举报
回复
引用 2 楼 flashyyp123 的回复:
listView1.Items.Add 哪个循环加断点 调试
加断点看数据也没发现问题啊?
wzsb2010 2018-04-28
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
你贴的代码和你描述的问题关系不大。 个人感觉,可能返回的列名和显示的列名不匹配
---------------------------------------------------------------------- 我也怀疑过可能获取的行列不对,但在之前初始化页面的时候有个跟onclick 函数中 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); } 这段代码一样的代码 ,读取出来的数据是正常的,但点击查询按钮 之后很多行显示就只有第一列。但还有一些行显示正常。将从数据库中读出来的数据打印出来,也没发现 有问题~所以没啥头绪啊~

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧