列表框项目在将项目从上到下移动时保存在SQL Server中,反之亦然

时间:2014-11-04 06:32:46

标签: c# windows-forms-designer

在Windows窗体中

我有一个列表框和两个按钮UP和Down ..当我单击其中一个按钮时,列表项将向上或向下移动,具体取决于单击的按钮。

我的问题是,当我点击Down按钮时,listItem将向下方移动,同时它将在运行时更新为与listitems相同..我尝试了很多我的代码是如下所示..

private void btndown_Click(object sender, EventArgs e)
{
    //string myNextItem1 = frm2lstbx.Items[frm2lstbx.SelectedIndex + 1].ToString();

    string globalNextItem1 = "";

    {
        int a = frm2lstbx.Items.Count - 1;

        if (frm2lstbx.SelectedItem == null)
        {
            MessageBox.Show("Please select an Item");
        }
        else if (frm2lstbx.SelectedIndex == a)
        {
            MessageBox.Show("No Items to move Lower side");
        }
        else
        {
            int i = 0;
            i = frm2lstbx.SelectedIndex;
            string currItem = "";
            string nextItem = "";
            globalNextItem1= frm2lstbx.Items[i + 1].ToString();
            currItem = frm2lstbx.SelectedItem.ToString();

            frm2lstbx.Items.Insert(i, globalNextItem1.ToString());
            MyDAL.UpdateData1(currItem, globalNextItem1);
            MyDAL.UpdateData1(globalNextItem1, currItem);
            frm2lstbx.Items.RemoveAt(i + 2);
         }
     }
  }

MYDAL类中,我在SQL Server中有Updatedata的方法......

public static void UpdateData1(string UpdatedItem, string OldItem)
{
        string strSql = @" Update tbl_srs set [Description]='" + UpdatedItem + "' where [Description]='" + OldItem + "'";

        Utils.ExecuteSql(strSql);
}

注意:Utils是另一个类库,它包含执行sql命令的方法等等。

我的问题是:SQL Server正确更新所选项目下面的行..但所选项目覆盖相同的项目......这就是为什么整个列表框更新了所选项目的相同值...

例如:我已经采用i = 0索引的选定项目......

当我点击向下按钮控制转到i = 1 nd它现在将被选中项目,所以在SQL Server中我更新了i = 1索引项目,其中i = 0项目..正确更新

i = 0之后,索引项可能无法正确更新..覆盖相同的值,即i = 0Indexed项。

0 个答案:

没有答案