Getting the selected value of dynamic text fields and lists

I have winform in C # that I dynamically created two comboboxes and a text box. When the user selects the month and year and enters the value in the text box, I want to get the corresponding comboboxes values ​​when I click to save. By default, the month and year assembly will select the current month and year.

On the same screen there is another part where the data will be filled in for the previous month, for example, from January to March during the current year in lists and text fields, if available.

I am not sure if this approach is correct, or should I go with datagridview. Below is a screenshot and my code. Any suggestions on how I can do this.

Screenshot enter image description here

the code

private void createComboMonths()
{
   int width = 79;
   int height = 24;
   int spacing = 28;
   ComboBox[] SubMonths = new ComboBox[12];
   for (int i = 0; i <= 11; ++i)
   {
       SubMonths[i] = new ComboBox();
       SubMonths[i].Name = "SubMonths";
       SubMonths[i].DropDownStyle = ComboBoxStyle.DropDownList;
       SubMonths[i].Size = new Size(width, height);
       SubMonths[i].Location = new Point(56, (i * height) + spacing);
       SubMonths[i].Items.Add("January");
       SubMonths[i].Items.Add("February");
       SubMonths[i].Items.Add("March");
       SubMonths[i].Items.Add("April");
       SubMonths[i].Items.Add("May");
       SubMonths[i].Items.Add("June");
       SubMonths[i].Items.Add("July");
       SubMonths[i].Items.Add("August");
       SubMonths[i].Items.Add("September");
       SubMonths[i].Items.Add("October");
       SubMonths[i].Items.Add("November");
       SubMonths[i].Items.Add("December");
       SubMonths[i].SelectedItem = DateTime.Today.ToString("MMMM");
       plSubscription.Controls.Add(SubMonths[i]);

    }
}


private void createComboYears()
{
   int width = 79;
   int height = 24;
   int spacing = 28;
   ComboBox[] SubYears = new ComboBox[12];
   for (int i = 0; i <= 11; ++i)
   {
       SubYears[i] = new ComboBox();
       SubYears[i].Name = "SubYears";
       SubYears[i].DropDownStyle = ComboBoxStyle.DropDownList;
       SubYears[i].Size = new Size(width, height);
       SubYears[i].Location = new Point(145, (i * height) + spacing);
       plSubscription.Controls.Add(SubYears[i]);
       fillComboData(SubYears[i]); // Function to fill the last 5 years
    }
}



private void createTextBoxes()
{
   int width = 79;
   int height = 24;
   int spacing = 28;
   TextBox[] subAmt = new TextBox[12];
   for (int i = 0; i <= 11; ++i)
   {
      subAmt[i] = new TextBox();
      subAmt[i].Name = "SubAmt" + i;
      subAmt[i].Border.Class = "TextBoxBorder";
      subAmt[i].Size = new Size(width, height);
      subAmt[i].Margin = new Padding(10, 10, 10, 10);
      subAmt[i].Location = new Point(279, (i * height) + spacing);
      subAmt[i].KeyPress += new KeyPressEventHandler(txtJanAmt_KeyPress);
      plSubscription.Controls.Add(subAmt[i]);

    }
 }


private void btnSave_Click(object sender, EventArgs e)
{
    DataTable dtSubs = new DataTable();
    dtSubs.Columns.Add("SubscriberID", typeof(string));
    dtSubs.Columns.Add("Month", typeof(string));
    dtSubs.Columns.Add("Year", typeof(string));
    dtSubs.Columns.Add("SubAmt", typeof(string));
    DataRow row = dtSubs.NewRow(); 
    foreach (Control c in plSubscription.Controls)
    {
        //<- Not sure how do I get the selected row as in the screenshot
    }
}

EDIT 1

, datatable,

  • comboboxes , datatable
  • , datatable
  • , , , .

for (int i = 0; i < dt.Rows.Count; i++)
{
   #region Grid Column Names
   DataGridViewComboBoxColumn mntCmb = new DataGridViewComboBoxColumn();
   mntCmb.HeaderText = "Month";
   mntCmb.Name = "Month";
   mntCmb.DataSource = dt;
   mntCmb.DisplayMember = "paidformonth";
   mntCmb.ValueMember = "paidformonth";
   // <-How do I set the column as selected.

   DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
   yearCmb.HeaderText = "Year";
   yearCmb.Name = "Year";
   yearCmb.DisplayMember = "paidforyear";
   yearCmb.ValueMember = "paidforyear";
   // <-How do I set the column as selected.

   DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
   amount.HeaderText = "Subscription Amount";
   amount.Name = "Subscription Amount";
   amount.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
   // <-How do I set this column with the value from the datatable

   #endregion

   dgvSubscriptions.Columns.AddRange(mntCmb, yearCmb, amount);
}

2

, , 3 6 . 2 3 . . , , . . .

dgvSubscriptions.AllowUserToAddRows = true

, . , , - /, , , , , , , .

enter image description here

3

DataPropertyName ValueMember,

DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
yearCmb.HeaderText = "Year";
yearCmb.Name = "Year";
//yearCmb.DataSource = dt;
yearCmb.DisplayMember = "paidforyear";
//yearCmb.ValueMember = "paidforyear";
yearCmb.DataPropertyName= "paidforyear";
yearCmb.DefaultCellStyle.NullValue = dt.Rows[i][2].ToString();
yearCmb.ReadOnly = true;
dgvSubscriptions.Columns.Add(yearCmb);

4

,

dgvSubscriptions.AutoGenerateColumns = false;
dgvSubscriptions.ColumnCount = 1;
dgvSubscriptions.Columns[0].Name = "ID";
dgvSubscriptions.Rows.Clear();
for (int i = 0; i <dt.Rows.Count; i++)
{
     dgvSubscriptions.Rows.Add();
     #region Grid Column Names
     DataGridViewComboBoxColumn mntCmb = new DataGridViewComboBoxColumn();
     mntCmb.HeaderText = "Month";
     mntCmb.Name = "Month";
     //mntCmb.DataSource = dt;
     mntCmb.DisplayMember = "paidformonth";
     mntCmb.DataPropertyName = "paidformonth";
     //mntCmb.ValueMember = "paidformonth";
     mntCmb.DefaultCellStyle.NullValue = dt.Rows[i][1].ToString();
     mntCmb.ReadOnly = true;
     dgvSubscriptions.Columns.Add(mntCmb);


     DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
     yearCmb.HeaderText = "Year";
     yearCmb.Name = "Year";
     //yearCmb.DataSource = dt;
     yearCmb.DisplayMember = "paidforyear";
     //yearCmb.ValueMember = "paidforyear";
     yearCmb.DataPropertyName= "paidforyear";
     yearCmb.DefaultCellStyle.NullValue = dt.Rows[i][2].ToString();
     yearCmb.ReadOnly = true;
     dgvSubscriptions.Columns.Add(yearCmb);

     DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
     amount.HeaderText = "Subscription Amount";
     amount.Name = "Subscription Amount";
     amount.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
     //amount.DataPropertyName = dt.Rows[i][2].ToString();
     amount.DataPropertyName="subamount";
     amount.DefaultCellStyle.NullValue = dt.Rows[i][0].ToString();
     amount.ReadOnly = true;
     dgvSubscriptions.Columns.Add(amount);

     #endregion

}

5

IRSOG , - .

public struct Data
{
    public List<string> Mon { get; set; }
    public List<string> Year { get; set; }
}

private void fillGridData(DataTable dt)
{
  List<string> Mon = new List<string>() { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

  List<string> Year = new List<string>();
  int CurrentYear = DateTime.UtcNow.Year;
  int NextYear = CurrentYear + 1;
  int LastFiveYears = CurrentYear - 5;
  for (int i = LastFiveYears; i <= NextYear; i++)
  {
     Year.Add(i.ToString());
  }
  List<Data> _Data = new List<Data>();
  for (int i = 1; i <= 12; i++)
  {
     _Data.Add(new Data() { Mon = Mon, Year = Year });
  }

   dgvSubscriptions.Rows.Clear();
   dgvSubscriptions.Refresh();
   dgvSubscriptions.Visible = true;
   dgvSubscriptions.ColumnHeadersDefaultCellStyle.Font = new Font("Trebuchet MS", 8F, FontStyle.Regular);
   dgvSubscriptions.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
   dgvSubscriptions.AutoResizeColumns();
   dgvSubscriptions.AllowUserToResizeColumns = true;
   dgvSubscriptions.AllowUserToOrderColumns = true;
   dgvSubscriptions.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
   dgvSubscriptions.Dock = DockStyle.None;
   dgvSubscriptions.BackgroundColor = this.BackColor;
   dgvSubscriptions.BorderStyle = BorderStyle.None;
   dgvSubscriptions.AllowUserToAddRows = true;


  // If dt.Rows.Count > 0 then show the data - do not allow to change existing data
  if (dt.Rows.Count > 0)
  {

    dgvSubscriptions.Rows.Clear();
    dgvSubscriptions.Refresh();

    #region Grid Column Names
    dgvSubscriptions.AutoGenerateColumns = false;
    dgvSubscriptions.Rows.Clear();
    DataGridViewComboBoxColumn mntCmb = new DataGridViewComboBoxColumn();
    mntCmb.HeaderText = "Month";
    mntCmb.Name = "Month";
    mntCmb.DataSource = Mon;
    mntCmb.DefaultCellStyle.NullValue = "";
    dgvSubscriptions.Columns.Add(mntCmb);

    DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
    yearCmb.HeaderText = "Year";
    yearCmb.Name = "Year";
    yearCmb.DataSource = Year;
    yearCmb.DefaultCellStyle.NullValue = "";
    dgvSubscriptions.Columns.Add(yearCmb);

    DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
    amount.HeaderText = "Subscription Amount";
    amount.Name = "Subscription Amount";
    amount.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    amount.DefaultCellStyle.NullValue = "";
    dgvSubscriptions.Columns.Add(amount);
    #endregion

    #region Populate Grid
    for (int i = 0; i <dt.Rows.Count; i++)
    {
       dgvSubscriptions.Rows.Add();

       dgvSubscriptions.Rows[i].Cells[0].Value = dt.Rows[i][1].ToString();  // Month
       dgvSubscriptions.Rows[i].Cells[0].ReadOnly = true; // do not allow the user to make changes
       dgvSubscriptions.Rows[i].Cells[1].Value = dt.Rows[i][2].ToString(); // Year
       dgvSubscriptions.Rows[i].Cells[1].ReadOnly = true; // do not allow the user to make changes
       dgvSubscriptions.Rows[i].Cells[2].Value = dt.Rows[i][0].ToString();  // Subscription amount
       dgvSubscriptions.Rows[i].Cells[2].ReadOnly = true; // do not allow the user to make changes

    }
    #endregion

  }
  else // We come here if dt.Rows.Count is 0 we allow the user to select and save 
  {

    #region Grid Column Names
    DataGridViewComboBoxColumn mntCmb = new DataGridViewComboBoxColumn();
    mntCmb.HeaderText = "Month";
    mntCmb.Name = "Month";
    mntCmb.DataSource = Mon;

    DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
    yearCmb.HeaderText = "Year";
    yearCmb.Name = "Year";
    yearCmb.DataSource = Year;

    DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
    amount.HeaderText = "Subscription Amount";
    amount.Name = "Subscription Amount";
    amount.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    #endregion
    dgvSubscriptions.Columns.AddRange(mntCmb, yearCmb, amount);

    dgvSubscriptions.DataSource = _Data;
  }
  dgvSubscriptions.RowsDefaultCellStyle.Font = new Font("Trebuchet MS", 8F, FontStyle.Regular);

}
+5
3

DataGridView :

GetCurrentRowValues .

public Form1()
        {
            InitializeComponent();
            dataGridView1.MultiSelect = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> Mon = new List<string>() { "January", "February", "March", "April", "May", " June", "July", "August", "September", "October", "November", "December" };
            List<string> Year = new List<string>() { "2001", "2002", "2003", "2004", "2005", "2006" };
            List<Data> _Data = new List<Data>();
            for (int i = 1; i <= 12; i++)
            {
                _Data.Add(new Data() { Mon = Mon, Year = Year });
            }
            DataGridViewComboBoxColumn moonCmb = new DataGridViewComboBoxColumn();
            moonCmb.HeaderText = "Month";
            moonCmb.Name = "Month";
            moonCmb.DataSource = Mon;

            DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
            yearCmb.HeaderText = "Year";
            yearCmb.Name = "Year";
            yearCmb.DataSource = Year;
            DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
            amount.HeaderText = "Amount";
            amount.Name = "Amount";
            dataGridView1.Columns.AddRange(moonCmb, yearCmb, amount);

            dataGridView1.DataSource = _Data;

        }

        private void GetCurrentRowValues()
        {
            var mon = dataGridView1.CurrentRow.Cells["Month"].Value;
            var year = dataGridView1.CurrentRow.Cells["Year"].Value;
            var amont = dataGridView1.CurrentRow.Cells["Amount"].Value;
        }

    }
    public struct Data
    {
        public List<string> Mon { get; set; }
        public List<string> Year { get; set; }
    }

enter image description here

        #region Grid Column Names
        dgvSubscriptions.AutoGenerateColumns = false;
        dgvSubscriptions.ColumnCount = 1;
        dgvSubscriptions.Columns[0].Name = "ID";
        dgvSubscriptions.Rows.Clear();

        DataGridViewComboBoxColumn mntCmb = new DataGridViewComboBoxColumn();
        mntCmb.HeaderText = "Month";
        mntCmb.Name = "Month";
        //mntCmb.DataSource = dt;
        mntCmb.DisplayMember = "paidformonth";
        mntCmb.DataPropertyName = "paidformonth";
        //mntCmb.ValueMember = "paidformonth";
        mntCmb.DefaultCellStyle.NullValue = "";
        mntCmb.ReadOnly = true;
        mntCmb.Items.Add("april");
        mntCmb.Items.Add("jun");
        mntCmb.Items.Add("jull");
        dgvSubscriptions.Columns.Add(mntCmb);

        DataGridViewComboBoxColumn yearCmb = new DataGridViewComboBoxColumn();
        yearCmb.HeaderText = "Year";
        yearCmb.Name = "Year";
        //yearCmb.DataSource = dt;
        yearCmb.DisplayMember = "paidforyear";
        //yearCmb.ValueMember = "paidforyear";
        yearCmb.DataPropertyName = "paidforyear";
        yearCmb.DefaultCellStyle.NullValue = "";
        yearCmb.Items.Add("2001");
        yearCmb.Items.Add("2002");
        yearCmb.Items.Add("2003");
        yearCmb.ReadOnly = true;
        dgvSubscriptions.Columns.Add(yearCmb);
        DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
        amount.HeaderText = "Subscription Amount";
        amount.Name = "Subscription Amount";
        amount.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        //amount.DataPropertyName = dt.Rows[i][2].ToString();
        amount.DataPropertyName = "subamount";
        amount.DefaultCellStyle.NullValue = "";
        amount.ReadOnly = true;
        dgvSubscriptions.Columns.Add(amount);
        #endregion

       for (int i = 0; i < dt.Rows.Count; i++)
        {
            dgvSubscriptions.Rows.Add();
            dgvSubscriptions.Rows[i].Cells[0].Value = dt.Rows[i][0].ToString();
            dgvSubscriptions.Rows[i].Cells[1].Value = dt.Rows[i][1].ToString();
            dgvSubscriptions.Rows[i].Cells[2].Value = dt.Rows[i][2].ToString();
            dgvSubscriptions.Rows[i].Cells[3].Value = dt.Rows[i][0].ToString();
        }
+2

ActiveControl .

var focusedCtrl = this.ActiveControl;
var siblings = Controls.Where(c => c.Location.Y == focusedCtrl.Location.Y).ToList();
foreach (Control c in siblings)
{
   // all thise controls are on the same row, providede that the allign
}

DataGridView. , , .

+1

I'd go to the DataGridView, but if you still want to keep this structure, you might want to update your KeyPressed event handler to look something like this.

private string lastUsedTextBox = string.Empty;
private string lastEnteredValue = string.Empty;

private void txtJanAmt_KeyPress(object sender, KeyPressEventArgs e)
{
lastUsedTextBox = (sender as TextBox).Name;                      
lastEnteredValue = (sender as TextBox).Text;                       
}

And then when you click the save button, you have everything that is stored here.

If you need to save all the data, you can use a dictionary or something like that.

+1
source

All Articles