Listview 与datagridview的数据更新差异点
作为新鸟的我,这两天试着做个小程序,用listView 控件显示数据,但是数据再后台base 刷新后,总是无法同步的更新
控件数据。前期习惯用datagridview 显示数据,只需要一个datatable绑定DataSource,在刷新datatable 的同时,前台的
控件数据也同步刷新,这次傻了。。。。。
错误代码:
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e) { if (this.tabControl1.SelectedTab.Text == "出租") { string strLendText = "select User_IDS,User_nameS,User_homePhone,User_cardID,User_phone from tb_User where User_type='lend'"; fillDateWithTable(strLendText); } else if (this.tabControl1.SelectedTab.Text == "求租") { string strLendText = "select User_IDS,User_nameS,User_homePhone,User_cardID,User_phone from tb_User where User_type='want'"; fillDateWithTable(strLendText); } }
经过几次尝试,发现在数据刷新前,先调用listview 的clear 方法,清空控件数据,重新填充,这样就可以实现数据的实时显示
lvUserInfo.Items.Clear();
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e) { lvUserInfo.Items.Clear(); if (this.tabControl1.SelectedTab.Text == "出租") { string strLendText = "select User_IDS,User_nameS,User_homePhone,User_cardID,User_phone from tb_User where User_type='lend'"; fillDateWithTable(strLendText); } else if (this.tabControl1.SelectedTab.Text == "求租") { string strLendText = "select User_IDS,User_nameS,User_homePhone,User_cardID,User_phone from tb_User where User_type='want'"; fillDateWithTable(strLendText); } }