Click here to Skip to main content
15,906,816 members
Articles / Programming Languages / C#
Tip/Trick

Add Values in Gridview Using ENTER KEY

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
2 Jul 2015CPOL 15K   7   8
This tip shows you how to add the Values in Gridview using ENTER key...

Introduction

In this tip, I am going to show you how to add the values in gridview by pressing ENTER KEY.

Using the Code

Create an Empty Windows Form. Add a Datagridview and Textbox in the Form.

Select Key Press Event in Property Window and Name method. Paste the below code. That may do the work:

C#
void TextBox1KeyPress(object sender, KeyPressEventArgs e)
  {
      if (e.KeyChar == (char)13)
      {
          {
          string dates= textBox1.Text.ToString();  // assign the Textbox value to a string
          int i=dataGridView1.Rows.Count;  // count the Number of available rows in the GridView
          int j=i-1;              //counted the Rows minus 1 that will make you add Rows
          this.dataGridView1.Rows.Add(); // create the Rows if it is needed
          dataGridView1.Rows[j].Cells[0].Value=dates; // add the Values to the GridView
          textBox1.Text=""; //Empty the Textbox
          }
      }

Let's explain the code line by line:

C#
int i=dataGridView1.Rows.Count; 

This line allows to Count the Number of rows available in the Gridview and it has been assigned to the Variable i.

C#
this.dataGridView1.Rows.Add();

This code allows to create only one Row at a time.

C#
dataGridView1.Rows[j].Cells[0].Value=dates; 

This allows us to Add the value to the gridview.

C#
dataGridView1.Rows[RowIndex].Cells[CellIndex].Value;

Row Index may change according to the values available in the Gridview.

Or we can ignore all the above lines and use the line below.

It allow us add the Data and create Rows in the Gridview.

C#
dataGridView.Rows.Add(datas);

I think this may help all of you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionMuito Bom. Pin
Junior Sousa7-Jul-15 4:13
Junior Sousa7-Jul-15 4:13 
GeneralRe: Muito Bom. Pin
Mohammed Ibrahim.L7-Jul-15 18:55
professionalMohammed Ibrahim.L7-Jul-15 18:55 
thats great dude. I think your code cannot able to add value to multiple columns. Is that possible??Confused | :confused: Confused | :confused:
GeneralRe: Muito Bom. Pin
Junior Sousa8-Jul-15 7:05
Junior Sousa8-Jul-15 7:05 
GeneralRe: Muito Bom. Pin
Mohammed Ibrahim.L8-Jul-15 7:26
professionalMohammed Ibrahim.L8-Jul-15 7:26 
GeneralGood One Pin
aarif moh shaikh2-Jul-15 18:45
professionalaarif moh shaikh2-Jul-15 18:45 
GeneralRe: Good One Pin
Mohammed Ibrahim.L2-Jul-15 23:21
professionalMohammed Ibrahim.L2-Jul-15 23:21 
GeneralThat's cool! Pin
Sanpath Sunggad2-Jul-15 12:44
Sanpath Sunggad2-Jul-15 12:44 
GeneralRe: That's cool! Pin
Mohammed Ibrahim.L2-Jul-15 12:48
professionalMohammed Ibrahim.L2-Jul-15 12:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.