首次单击时不更新ListBox数据源 [英] ListBox datasource is not updated on first click

查看:69
本文介绍了首次单击时不更新ListBox数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个winform程序,它将ms访问数据库中的数据加载到我的列表框中。当我点击我想要更新我的列表框上的数据库和数据源的项目并单击更新按钮时,该项目第一次点击时不会更新,但第二次(在同一项目上),所以每次我需要修改我的数据我需要选择我的项目并单击两次更新按钮。那是疯了大声笑..



<前lang =c#> 私人 void Fill()
{
string strSQL = SELECT * FROM Item ORDER BY ITEM;
OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL,GetConnection());
DataSet dtSet = new DataSet();
myCmd.Fill(dtSet, Item);
DataTable dTable = dtSet.Tables [ 0 ];
foreach (DataRow dtRow in dTable.Rows)
{
_productlist.Add( new PRODUCTLIST(){ID = dtRow [ ID]。ToString(),ITEM = dtRow [ ITEM] .ToString(),ITEM_DESC = dtRow [ ITEM_DESC]。ToString()});
}
listBox1.DisplayMember = ITEM;
listBox1.DataSource = _productlist;
listBox1.ValueMember = ID;
GetConnection()。Close();
}

private void Form1_Load( object sender,EventArgs e)
{
// 使ID文本框不可编辑
textBoxID.Enabled = false ;

// 填写列表框
Fill();

textBoxID.DataBindings.Add( Text,_ productlist, ID false ,DataSourceUpdateMode.Never) ;
textBoxITEM.DataBindings.Add( Text,_ productctlist, ITEM false ,DataSourceUpdateMode.Never);
textBoxITEMDESC.DataBindings.Add( Text,_ productctlist, ITEM_DESC false ,DataSourceUpdateMode.Never);
}

私有 void buttonUpdate_Click( object sender,EventArgs e)
{
// 更新数据库
OleDbDataAdapter cmd = new OleDbDataAdapter();
cmd.UpdateCommand = new OleDbCommand( UPDATE Item SET ITEM = @ITEM,ITEM_DESC = @ITEM_DESC WHERE ID = @ID,GetConnection());
cmd.UpdateCommand.Parameters.AddWithValue( @ ITEM,textBoxITEM.Text);
cmd.UpdateCommand.Parameters.AddWithValue( @ ITEM_DESC,textBoxITEMDESC.Text);
cmd.UpdateCommand.Parameters.AddWithValue( @ ID,Convert.ToInt32(textBoxID 。文本));
cmd.UpdateCommand.ExecuteNonQuery();

// 更新数据源
_productlist.Clear() ;
listBox1.DataSource = null ;
listBox1.Items.Clear();
Fill();
listBox1.Update();
}







基本的事情发生:



1.使用数据绑定到文本框将数据加载到列表框

2.单击列表框中的项目并从文本框中修改数据

3.单击更新用于更新数据库和数据源的按钮

4.数据源第一次不更新但数据库确实更新

5.重复2

6重复3号

7.数据源更新



我想要的是什么:



1.使用数据绑定到文本框将数据加载到列表框

2.单击列表框中的项目并从文本框中修改数据

3.单击更新按钮更新数据库和数据源

4.数据源和数据库更新



如何在我的应用程序上修复此错误?如果我需要更清楚,请告诉我..

解决方案

在数据库中更新值后。

尝试检索并使用Listbox再次绑定数据。


这是我用来更新数据源的内容



//清除项目源

_productlist.Clear();

//将datasource设置为null

listBox1.DataSource = null;

//等待半秒以使数据库更新>。<< br mode =hold/>System.Threading.Thread.Sleep(500);

//重新填充半秒后的列表框

Fill();


i create a winform program that load the data from ms access database to my listbox. when i click my item which i want to update on database and datasource on my listbox and click the update button, the item just wont update for the first time clicked but the second does(on the same item), so everytime i need to modify my data i need to choose my item and click the update button two times. that''s is crazy lol..

private void Fill()
        {
            string strSQL = "SELECT * FROM Item ORDER BY ITEM";
            OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, GetConnection());
            DataSet dtSet = new DataSet();
            myCmd.Fill(dtSet, "Item");
            DataTable dTable = dtSet.Tables[0];
            foreach (DataRow dtRow in dTable.Rows)
            {
                _productlist.Add(new PRODUCTLIST() { ID = dtRow["ID"].ToString(), ITEM = dtRow["ITEM"].ToString(), ITEM_DESC = dtRow["ITEM_DESC"].ToString() });
            }
            listBox1.DisplayMember = "ITEM";
            listBox1.DataSource = _productlist;
            listBox1.ValueMember = "ID";
            GetConnection().Close();
        }

    private void Form1_Load(object sender, EventArgs e)
        {
            // make ID textbox un editable
            textBoxID.Enabled = false;

            // fill the listbox
            Fill();

            textBoxID.DataBindings.Add("Text", _productlist, "ID", false, DataSourceUpdateMode.Never);
            textBoxITEM.DataBindings.Add("Text", _productlist, "ITEM", false, DataSourceUpdateMode.Never);
            textBoxITEMDESC.DataBindings.Add("Text", _productlist, "ITEM_DESC", false, DataSourceUpdateMode.Never);
        }

private void buttonUpdate_Click(object sender, EventArgs e)
        {
            //update the database
            OleDbDataAdapter cmd = new OleDbDataAdapter();
            cmd.UpdateCommand = new OleDbCommand("UPDATE Item SET ITEM = @ITEM, ITEM_DESC = @ITEM_DESC WHERE ID = @ID",GetConnection());
            cmd.UpdateCommand.Parameters.AddWithValue("@ITEM", textBoxITEM.Text);
            cmd.UpdateCommand.Parameters.AddWithValue("@ITEM_DESC", textBoxITEMDESC.Text);
            cmd.UpdateCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(textBoxID.Text));
            cmd.UpdateCommand.ExecuteNonQuery();

            // update the datasource
            _productlist.Clear();
            listBox1.DataSource = null;
            listBox1.Items.Clear();
            Fill();
            listBox1.Update();
        }




basic thing that happen:

1. load data to listbox with databind to textbox
2. click item on my listbox and modify data from textbox
3. click the update button to update database and datasource
4. datasource not update for the first time but database does update
5. repeat no.2
6. repeat no.3
7. datasource updated

what i want:

1. load data to listbox with databind to textbox
2. click item on my listbox and modify data from textbox
3. click the update button to update database and datasource
4. datasource and database updated

how do i fix this bug on my application?? please let me know if i need to be more clear..

解决方案

Once the values are updated in the databases.
Try to retrieve and bind the data again with Listbox.


this is what i use to update the datasource

// clear the item source
_productlist.Clear();
// set datasource to null
listBox1.DataSource = null;
// wait for half second for database to updated >.<<br mode="hold" />System.Threading.Thread.Sleep(500);
// re-populate the listbox after half second
Fill();


这篇关于首次单击时不更新ListBox数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆