简体   繁体   中英

uppercase items in listview C#

how can I change the character casing in my listview to uppercase? the items in listview should be in uppercase when I choose uppercase in combobox. I hope someone can help me with this. Thanks in advance.

 private void Form1_Load(object sender, EventArgs e)
    {
        showlv("SELECT a.customer_name, a.address, b.product_name, b.price FROM tbl_customer AS a INNER JOIN tbl_transaction AS b WHERE a.customer_code = b.customer_code", lvcust);
    }

    private void showlv(string sql, ListView lv)
    {
        try
        {
            lvcust.View = View.Details;
            lvcust.FullRowSelect = true;
            lvcust.GridLines = true;
            conn.Open();
            MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            conn.Close();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem lvitem = new ListViewItem(dr["customer_name"].ToString());
                lvitem.SubItems.Add(dr["address"].ToString());
                lvitem.SubItems.Add(dr["product_name"].ToString());
                lvitem.SubItems.Add(dr["price"].ToString());
                lvcust.Items.Add(lvitem);
            }

            string[] column = new string[4] { "Customer Name", "Address", "Product Name", "Price" };

            for (int x = 0; x < column.Length ; x++)
            {
                lvcust.Columns.Add(column[x]);
            }
        }
        catch (Exception er)
        {
            MessageBox.Show(er.Message);
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.Equals("Ascend"))
        {
            lvcust.Sorting = SortOrder.Ascending;
        }
        else if (comboBox1.SelectedItem.Equals("Descend"))
        {
            lvcust.Sorting = SortOrder.Descending;
        }
        else if (comboBox1.SelectedItem.Equals("Uppercase"))
        {
            //code to uppercase items in listview
        }
    }

You would be better off adding your case changing method in the event handler for the checkbox to upload it.

So, you doubleclick the checkbox control, then you iterate through the items in the combobox, then on each iteration set the content of the item to itself, with a .ToUpper() at the end.

I'm assuming you want to uppercase the customer name only. The trick is to store the original value as the tag of the ListItem. That way you can change the Text back to the original (non-uppercase) value later if you wanted. So in your code, find the first line and add the second below:

ListViewItem lvitem = new ListViewItem(dr["customer_name"].ToString());
lvitem.Tag =  dr["customer_name"].ToString();

Now that you have that, here's the for loop to convert it to upper case:

ListViewItemCollection items = lvcust.Items;
for(int i=0;i<items.Count;i++){
  ListViewItem item = items.Item[i];
  object tag = item.Tag;
  if(tag is string){
    item.Text = ((string)tag).ToUpper();
  }
}

This was all done off the top of my head in a text editor so there may be a syntax issue here or there but the logic should be correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM