Poab9200 6 Light Poster

Hello all, I'll do my best to make this as simple as possible to explain.

1. I'm using a WinForm DataGridView.
2. I'm using an auto complete feature that I've coded and I'm trying to add the data that a user has entered in the first column to a generic list.

Here is some of my code:
dataGridView_EditingControlShowing

if (dgvCommands)
            {
                DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
                te.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                te.AutoCompleteSource = AutoCompleteSource.CustomSource;

                switch (dgv_Alias.CurrentCell.ColumnIndex)
                {
                    case 0:
                        te.AutoCompleteCustomSource.Clear();
                        break;
                    case 1:
                        te.AutoCompleteCustomSource.Clear();
                        te.AutoCompleteCustomSource.AddRange(sCommands);
                        break;
                }
            }
            else
            { }

This is the code that loads the commands to the sCommands Array List.

private List<string> Commands = new List<string>();
private string[] sCommands;
private bool dgvCommands;

private void AC_LoadCommands()
        {
            if (File.Exists(@"data/cmds.txt"))
            {
                StreamReader sr = new StreamReader(@"data/cmds.txt");
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    if (!line.Contains("//"))
                    {
                        Commands.Add(line);
                    }
                    else
                    { }
                }

                sr.Close();

                for (int i = 0; i != Commands.Count; i++)
                {
                    sCommands = Commands.ToArray();
                }

                dgvCommands = true;
            }
            else
            {
                MessageBox.Show("Missing File: 'data/cmds.txt'\nAuto Complete cannot function without this file.", "Missing File: 'data/cmds.txt'", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dgvCommands = false;
            }

And that Method is loaded when the program starts under the Form_Loading Event.

Now I would like to gather the data from the first column and add it to the sCommands string array.

I need to know what events to use and a detailed example of how to go about doing so.

Any help would be greatly appreciated.

Thanks
- Poab9200

Dani AI

Generated

As already loads commands into a List<string> at startup, the simplest, reliable route is to capture completed edits in column 0, add a normalized unique string to that List, regenerate the string[] used for AutoComplete, and persist the list only when appropriate. Use DataGridView.CellEndEdit (or CellValidated/CellValidating when validation is needed) — CellEndEdit is straightforward for getting the committed value from a text cell.

Example (add in the grid’s CellEndEdit handler):

private void dgv_Alias_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0 || e.ColumnIndex != 0) return;

    var raw = dgv_Alias.Rows[e.RowIndex].Cells[0].Value as string;
    if (string.IsNullOrWhiteSpace(raw)) return;

    var entry = raw.Trim();
    if (!Commands.Exists(s => s.Equals(entry, StringComparison.OrdinalIgnoreCase)))
    {
        Commands.Add(entry);
        sCommands = Commands.ToArray(); // keep the array current for EditingControlShowing
    }
}

Keep disk I/O out of the hot path: rewrite the file on form close (or save periodically), not on every edit:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    File.WriteAllLines(Path.Combine(Application.StartupPath, "data", "cmds.txt"), Commands);
}

Notes and troubleshooting:

  • Avoid the per-iteration assignment pattern; call Commands.ToArray() once after changes.
  • Normalize and trim entries; use case-insensitive checks to prevent duplicates. A HashSet<string> (with an ordered List for UI) can speed lookups.
  • EditingControlShowing should rebuild the AutoCompleteCustomSource from Commands (or a new AutoCompleteStringCollection) each time editing starts so the latest entries appear.
  • For non-text cells (combo/checkbox) or edits that need immediate commit, consider CurrentCellDirtyStateChanged + CommitEdit, or use CellValidating to inspect e.FormattedValue.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.