Tutorial :Textbox control in DataGridView


I am working on a datagridview in C# in windows application. I want to add textbox controls in DataGridView. So, when we run it then textbox should be shown in gridview and we can put value in it and My grid has 3 columns and I want to add new row in grid when I press tab on 3rd column of gridview.

How do I do this?


It's hard to provide a precise answer since your question is lacking in detail and pretty general, but to get textboxes in your DataGridView, you're going to want to add some instances of DataGridViewTextBoxColumn to the DataGridView's Columns collection. This will cause them to be populated with textboxes in each row.

To detect when the user presses tab on the 3rd column, you can add a 1-2 pixel wide fourth column and detect that it has recieved focus (almost definitely from a tab keystroke) using the OnCellEnter event.

Good luck!


So, for the "display textboxes by default portion of your question, here's the skinny:

On GridView->Edit Columns, add the columns you want to use explicitly. Then click on the link "Convert this field into a templateField". This will let you tweak the generated HTML for those cells. Say OK. Then go to GridView->Edit Templates. For your favorite Column, copy the ItemEditTemplate into the ItemTemplate. (ItemTemplate is the default. ItemEditTemplate contains the properly bound edit control.) Now all of your data fields will default to "editable."

I'm guessing you have a submit button. You'll need to tell the GridView to update the rows on submit., like so:

    For Each r As GridViewRow In GridView1.Rows          Dim mon = System.Int32.Parse(CType(r.FindControl("TextBox1"), TextBox).Text)          If mon <> 0 Then GridView1.UpdateRow(r.RowIndex, False)      Next  

Obviously, you'll want different logic inside there, but the basic loop/findControl/updateRow logic should apply.

Microsoft has a walkthrough on this here: Performing Bulk Updates to Rows Bound to a GridView

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »