简体   繁体   中英

While binding combobox how could I get binded data in uppercase format in c#.net

I want to bind the combobox in c#.net windows application and that also the binded combobox I want in uppercase word.

Now, I bind the combobox successfully but the problem is that I didnt get any uppercase word in it.

Here is my code,

public void BindDropdownList(ComboBox f_dropdown, string tblname, string display_field, string value_fldName, string wherecondition = "")
{
    try
    {
        string qrysel = "select " + value_fldName + "," + display_field + " from " + tblname + " " + wherecondition + "";
        DataTable dt_list_detail = new DataTable();

        dt_list_detail = clsObjDataAccess.GetDataTable(qrysel);

        if (dt_list_detail != null)
        {
            if (dt_list_detail.Rows.Count > 0)
            {
                f_dropdown.DataSource = dt_list_detail;
                f_dropdown.DisplayMember = display_field;
                f_dropdown.ValueMember = value_fldName;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

I tried many things but didnt work like

f_dropdown.DataSource = dt_list_detail;
f_dropdown.DisplayMember = display_field.ToUpper();
f_dropdown.ValueMember = value_fldName.ToUpper();

next

f_dropdown.DisplayMember = display_field.ToString().ToUpperInvariant();

and many other thing also but the same thing happing with me that is not working in upper case word.

You could do the following.

f_dropdown.Format += (s, arg) =>
{
  arg.Value = arg.Value.ToString().ToUpperInvariant();
};

Test Collection

_persons = new List<Person>
 {
    new Person(){Id=1, Name = "Anu"},
    new Person(){Id=1, Name = "Jia"},
 };



f_dropdown.DataSource = 
f_dropdown.DisplayMember = "Name";
f_dropdown.Format += (s, arg) =>
{
  arg.Value = arg.Value.ToString().ToUpperInvariant();
};

Output

在此处输入图片说明

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