Deepak
Deepak

Reputation: 7917

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?

Upvotes: 0

Views: 11315

Answers (3)

user13125135
user13125135

Reputation: 1

Try this, for example if you want to set your first column in datagridview as a textbox control:

private void dtgrdview_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        TextBox txtbox1=new TextBox();
        dtgrdview.Controls.Add(txtbox1);
        Rectangle rectangle = dtgrdview.GetCellDisplayRectangle(0, e.RowIndex, true);
        txtbox1.Location = rectangle.Location;
        txtbox1.Size = rectangle.Size;                
        txtbox1.TextChanged += txtbox1_TextChanged; 
        txtbox1.Leave += txtbox1_Leave;                
        txtbox1.Visible = true;
    }

}

and don't forget to add this event in the same class as like below to call it when the cell have the focus:

private void txtbox1_Leave(object sender, EventArgs e)
{
    txtbox1.Visible = false;
}

private void txtbox1_TextChanged(object sender, EventArgs e)
{
    dtgrdview.CurrentCell.Value = txtbox1.Text;
}

If you have any other questions, don't hesitate to ask me :)

Upvotes: 0

Sean McMillan
Sean McMillan

Reputation: 10093

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

Upvotes: 0

Katelyn Gadd
Katelyn Gadd

Reputation: 1393

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!

Upvotes: 2

Related Questions