1

I have a table where I'm saving payments each user made for each month and originally I had column named month but after saving couple of payments for every month it would become too clustered so I decided to ditch column month and add 12 columns named (january, february, etc. all the way to december) and now I want to display all 12 columns in combobox so I can select January and make payment for that month.

void FillCombo()
    {
        string constring = "server=localhost;user id=David;password=root;persistsecurityinfo=True;database=bazakudsumari";
        string Query = "SELECT * FROM bazakudsumari.evidencija_clanarina;";
        MySqlConnection connection = new MySqlConnection(constring);
        MySqlCommand command = new MySqlCommand(Query, connection);
        MySqlDataReader myReader;

        try
        {
            connection.Open();
            myReader = command.ExecuteReader();
            while (myReader.Read())
            {
                string sSijecanj = myReader.GetString("sijecanj");
                odaberi_mjesec.Items.Add(sSijecanj);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Any way of doing this?

  • @Eric Yes, but I'm not trying to add values from columns, I need to fill combobox with column names (January, February, etc. all the way to December) and then when I select let's say January and make payment that it inserts data only in that column, January. If you can get what I'm asking. :) – David Hruškar Sep 26 '19 at 19:14
  • The month's names are well known, why do you want to do this task reading a database table schema instead of adding directly the month's names? – Steve Sep 26 '19 at 19:15
  • @Steve Well, because when displaying all users who payed couple of months in a datagridview then the data is clustered as hell, hard to read and find what you want. So this would be a cleaner solution, at least in my opinion. – David Hruškar Sep 26 '19 at 19:18
  • https://stackoverflow.com/questions/681653/can-you-get-the-column-names-from-a-sqldatareader Maybe this will help for getting the column names? – Dortimer Sep 26 '19 at 19:20
  • It is a lot more complicated. However if you want to do so take a look at the [INFORMATION_SCHEMA.COLUMNS](https://dev.mysql.com/doc/refman/5.7/en/columns-table.html) – Steve Sep 26 '19 at 19:21
  • Okay, so I wrote the code like this public void FillComboBox() { odaberi_mjesec.Items.Add("January").ToString(); } and it displays it how I wanted. – David Hruškar Sep 26 '19 at 19:24
  • _odaberi_mjesec.Items.AddRange(CultureInfo.CurrentCulture.DateTimeFormat.MonthNames);_ – Steve Sep 26 '19 at 19:26
  • @Steve Oh, I like your solution even more. Any way to make first letter capital? – David Hruškar Sep 26 '19 at 19:30

1 Answers1

0

You can simply add the months names reading them directly from your CultureInfo

var months = CultureInfo.CurrentCulture
                        .DateTimeFormat.MonthNames.Select(x =>  
             CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x)).ToArray();
odaberi_mjesec.Items.AddRange(months);
Steve
  • 213,761
  • 22
  • 232
  • 286