0

In C# with LiteDB i working on a little project, I'm searching for a way to add entry to a datagridview with 5 columns + 1 Textbox.

On datagridView, i have a colID so if i enter on textbox dev5 dgv select this row and i can write on their row like this example:

on textbox i type :

dev5 [Enter] -> line selected by dgv -> 2 [Enter] -> 5 [Enter]..

 |colID |col2|col3|col4|col5|
 |--------------------------|
 |dev3  |    |    |    |    |
 |dev4  |    |    |    |    |
 |--------------------------|
*|dev5  | 2  |  5 |    |    |
 |--------------------------|
 |dev6  |    |    |    |    |
------------------------------

I need to store value of textbox on [Enter] to a array like {dev5,2,5,"",""}

and Insert to database in one shot because Actually, on every [enter] its update my table like this :

dev5;2
dev5;2;5
dev5;2;5;8
dev5;2;5;8;9

Like you see, it's not optimized so for a better result but i need just the last line..

Any idea for solving this problem are welcome

Kate
  • 320
  • 1
  • 6
  • 19
  • In theory, could you simply capture the contents of the textbox when enter is pressed and store those values in a list? The list should be pretty easy to iterate over. – JD Davis Sep 15 '17 at 20:37
  • @JDDavis yes interesting approach!but how i can store it to my list with 1 textbox if i click [Enter] 3 times with different value each time? – Kate Sep 15 '17 at 20:54
  • You only need to ensure you clear the textbox each time enter is pressed, it should be relatively straightforward to update the list each time. – JD Davis Sep 15 '17 at 21:03

1 Answers1

1

One potential method would look something like this

public List<string> Values = new List<string>();
public void UpdateValues()
{
    var value = textBox1.Text;

    Values.Add(value);
    textBox1.Clear();

}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char) Keys.Return)
    {
        UpdateValues();
    }
}
JD Davis
  • 3,517
  • 4
  • 28
  • 61
  • ok, i found my problem!! I'm declaring my List<> inside my textbox so it's not working before because we need to declare outside of my method. thanks for your example it's perfect!!! – Kate Sep 15 '17 at 21:18