Winforms列表框列表项

时间:2017-08-31 18:33:46

标签: winforms

public partial class Form1EOD : Form
{
    List<string> _items = new List<string>();
    public  int ItemHeight { get; set; }
    public Form1EOD()
    {
        InitializeComponent();
        AutoSizeMode= AutoSizeMode.GrowAndShrink;
        listBox1.ItemHeight = 30;
    }

    private DataTable PSIDKey()
    {
        ASIDatabase dbconnection = new ASIDatabase(ASIDatabase._ConnectionType.Client);
        string query = string.Format(DigActionsQueries.GET_EOD_PSID, textBox1.Text);
        return dbconnection.getDataTableFromQuery(query);
    }

    public static DataTable Pivot(DataTable tbl)
    {
        var tblPivot = new DataTable();
        tblPivot.Columns.Add(tbl.Columns[0].ColumnName);
        for (int i = 1; i < tbl.Rows.Count; i++)
        {
            tblPivot.Columns.Add(tbl.Columns.Add(Convert.ToString(i)));
        }
        for (int col = 0; col < tbl.Columns.Count; col++)
        {
            var r = tblPivot.NewRow();
            r[0] = tbl.Columns[col].ToString();
            for (int j = 1; j < tbl.Rows.Count; j++)
                r[j] = tbl.Rows[j][col];
            tblPivot.Rows.Add(r);
        }
        return tblPivot;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(textBox1.Text).Trim().Length <= 0)
        {
            MessageBox.Show("please enter PSID KEY");
            return;
        }
        DataTable dr = PSIDKey();
        foreach (DataRow row in dr.Rows)
        {
            foreach (DataColumn column in dr.Columns)
            {
                if (Convert.ToString(row[column].ToString()).Length > 0)
                {
                    listBox1.Items.Add(Convert.ToString(column.ColumnName));
                }
            }
        }
    }

    private string str = string.Empty;
    StringBuilder builder = new StringBuilder();

    private void button2_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(textBox1.Text).Trim().Length <= 0)
        {
            MessageBox.Show("please enter PSID Key");
            return;
        }

            for (int i = listBox1.SelectedIndex; i < listBox1.Items.Count; i++)
            {
                {
                    string strdata = listBox1.Items[i].ToString();
                    strdata = listBox1.Items[i].ToString();  
                    str = str + strdata + "=" + getFormattedValue("null") + ",";
                }
            }
            str = str.Substring(0, str.Length - 1);
            string query = "update asiAudit.dbo.EOD set " + str + " where EODPSID= " +
                           Convert.ToInt32(textBox1.Text);
            ASIDatabase clientConnection = new ASIDatabase(ASIDatabase._ConnectionType.Client);
            clientConnection.insertDatafromQuery(query);
            MessageBox.Show("clear sucessfully");
            //listBox1.Items.Clear();
            //textBox1.Text = String.Empty;
        }

        private static string getFormattedValue(string val)
        {
            if (val == "null")
                return val;
            else
                return string.Format("'{0}'", val);
        }
    }
}

这是我的winform代码, 当我点击提交按钮时,它会将动态列添加到列表框中 清除按钮将清除所选列表项的字段 我的代码工作正常,但是当我用别名更改我的选择查询列名时,更新查询不起作用 这是我的选择查询

public static string GET_EOD_PSID = @"select
      EODPlasticPrinted AS [Plastic Printed], 
      EODLaminate AS [Lamniate & Die Cut],
      EODEncoded AS [Encode], 
      EODHotStamped AS [Hot Stamp],
      EODEMVMilling AS [EMV Milling], 
      EODEmbossed AS [Embosing], 
      EODActivation AS [Activation Labels], 
      EODPlasticComplete AS [Final Count], 
      EODGVStaged AS [Match/Atach],
      EODGVComplete AS [Insertion], 
      EODAuditIn AS [Audit In],
      EODAuditComplete AS [Audit Out],
      EODAutoBagger AS [Autobaggeer],
      EODShippedIn AS [Shipped In],
      EODComplete AS [Complete]
    From
      asiAudit.dbo.EOD WITH(NoLock)
    WHERE 
       EODPSID in ({0})";

1 个答案:

答案 0 :(得分:0)

 public partial class Form1EOD : Form
    {
        public Form1EOD()
        {
            InitializeComponent();
        }

        private DataTable PSIDKey()
        {
            ASIDatabase dbconnection = new ASIDatabase(ASIDatabase._ConnectionType.Client);
            string query = string.Format(DigActionsQueries.GET_EOD_PSID, textBox1.Text);
            return dbconnection.getDataTableFromQuery(query);
        }
        public List<ItemType> ColumnsValues()
        {
            List<ItemType> columnlist = new List<ItemType>();
            columnlist.Add(new ItemType { ItemName = "EODPlasticPrinted", ItemValue = "Plastic Printed" });
            columnlist.Add(new ItemType { ItemName = "EODLaminate", ItemValue = "Laminate & Die Cut" });
            columnlist.Add(new ItemType { ItemName = "EODEncoded", ItemValue = "Encoder" });
            columnlist.Add(new ItemType { ItemName = "EODHotStamped", ItemValue = "Hot Stamp" });
            columnlist.Add(new ItemType { ItemName = "EODEMVMilling", ItemValue = "EMV Milling" });
            columnlist.Add(new ItemType { ItemName = "EODEmbossed", ItemValue = "Embossing" });
            columnlist.Add(new ItemType { ItemName = "EODActivation", ItemValue = "Activation Labels" });
            columnlist.Add(new ItemType { ItemName = "EODPlasticComplete", ItemValue = "Plastic Final Count" });
            columnlist.Add(new ItemType { ItemName = "EODGVStaged", ItemValue = "Match/Attach" });
            columnlist.Add(new ItemType { ItemName = "EODGVComplete", ItemValue = "Insertion" });
            columnlist.Add(new ItemType { ItemName = "EODAudiIn", ItemValue = "AuditIn" });
            columnlist.Add(new ItemType { ItemName = "EODAuditComplete", ItemValue = "AuditOut" });
            columnlist.Add(new ItemType { ItemName = "EODAutoBagger", ItemValue = "Autobagger" });
            columnlist.Add(new ItemType { ItemName = "EODShippedIn", ItemValue = "ShippedIn" });
            columnlist.Add(new ItemType { ItemName = "EODComplete", ItemValue = "Complete" });
            return columnlist;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(textBox1.Text).Trim().Length <= 0)
            {
                MessageBox.Show("please enter PSID KEY");
                return;
            }
            DataTable dr = PSIDKey();
            List<ItemType> dynList = new List<ItemType>();

            dynList = ColumnsValues();
            List<ItemType> itemlist = new List<ItemType>();
            foreach (DataRow row in dr.Rows)
            {
                foreach (DataColumn column in dr.Columns)
                {
                    if (Convert.ToString(row[column].ToString()).Length > 0)
                    {
                        string columnvalue = column.ColumnName;
                        ItemType data = new ItemType(); 
                        data = dynList.SingleOrDefault(x => x.ItemName == columnvalue);
                        itemlist.Add(data);
                    }
                }
            }
            itemlist = itemlist.Where(i => i != null).ToList();
            listBox1.DataSource = itemlist;
            listBox1.DisplayMember = "ItemValue";
            listBox1.ValueMember = "ItemName";
        }

        private string str = string.Empty;
        StringBuilder builder = new StringBuilder();

        private void button2_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(textBox1.Text).Trim().Length <= 0) 
            {
                MessageBox.Show("please enter PSID Key");
                return;
            } 
            for (int i = listBox1.SelectedIndex; i < listBox1.Items.Count; i++)
            { 
                { var data = (ItemType)listBox1.Items[i]; 
                string strdata = data.ItemName;
                str = str + strdata + "=" + getFormattedValue("null") + ","; 
                } 
            } 
            str = str.Substring(0, str.Length - 1); 
            string query = "update asiAudit.dbo.EOD set " + str + " where EODPSID= " + Convert.ToInt32(textBox1.Text); 
            ASIDatabase clientConnection = new ASIDatabase(ASIDatabase._ConnectionType.Client); 
            clientConnection.insertDatafromQuery(query); 
            MessageBox.Show("clear sucessfully");
           // listBox1.Items.Clear(); 
            textBox1.Text = String.Empty;
        }

        private static string getFormattedValue(string val)
        {
            if (val == "null")
                return val;
            else
                return string.Format("'{0}'", val);
        }
    }
    public class ItemType
    {
        public string ItemName { get; set; }
        public string ItemValue { get; set; }
    }
}