Textbox control in DataGridView - c#

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

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 :)

Related

Show the selected row record in textbox/label

How do I make the selected row record from a DataGridView show in a TextBox? I got some TextBox and Label in a Form. I want the text inside the TextBox/Label to change when the user selects a row record from the DataGridView. I tried the following code to make it happen, but it doesn't work
private void ItemTable_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
label_itemid_show.Text = ItemTable.Rows[e.RowIndex].Cells[0].Value.ToString();
text_itemname.Text = ItemTable.Rows[e.RowIndex].Cells[1].Value.ToString();
text_itemprice.Text = ItemTable.Rows[e.RowIndex].Cells[2].Value.ToString();
text_itemstock.Text = ItemTable.Rows[e.RowIndex].Cells[3].Value.ToString();
}
I'm working on a project that includes a features similar to yours. Currently I select a record in a datagridview and need to show it's values to textboxes so that any values can be edited.
This is how I tackle the problem.
Models.Item item = DAL.ItemDAL.GetItem(Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[0].Value));
tbModifyItemID.Text = Convert.ToString(item.ItemID);
tbModifyItemName.Text = item.ItemName;
It's the ItemDataGridView.CurrentRow.Cells[0].Value that finds me the ItemID of the selected record.
In your case it's [DataGridName].CurrentRow.Cells[FieldNo, starting at 0].Value.ToString();
This allows you to click on any cell in the record and still retrieve the specified cell in the code.
Hope it helps c:
PS:
If you put it inside a
private void ItemDataGrid_SelectionChanged(object sender, EventArgs e)
method it will run the code anytime you click a different cell in the datagridview.
You can use the following:
lblDetails.Text = dgMainGrid.SelectedRows[0].Cells[1].Value.ToString();
lblDetails: A label on the form.
dgMainGrid: DataGrivView on the form.
PS: Just make sure you select the row inside the datagrid and not any particular cell, row selecting is done by clicking on the left of your first column i.e. the row selector column which gets added at the run time in WinForms.

How can I scroll up my DataGridview when I click the content?

My table has many rows. I keeps some row numbers.
When I click No.3 , I want to display my table from No.3 and No. 1 and 2 must be scroll up.
But whatever I do, it is showing from 1,2,3, and can't can't scroll..
I want to write by using DataGrid_ItemClick(){}.
With Jquery .scrollTop(value) the page can be scrolled to the row entered. Here is a detailed way to use it If you are using datagrid in Jquery.
Try this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentRow.Index;
}
It will work no matter how the selection modes are set or whether the user holds ctrl or shift..

Add a column in gridview at run time

I want to add a new column in grid view at run time and in this column I want to add a button in some specific rows. How can I do this?
Please suggest me some relevant solution as soon as possible.
Although you can programmatically add column fields to the Columns collection, it is easier to list the column fields declaratively in the GridView control and then use the Visible property of each column field to show or hide each column field.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.100).aspx
So you see what you should do is go ahead and create the column in your GridView, but set the Visible property to False. Then to control the display of the column, you can make use of the GridView.RowDataBound Event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.100).aspx
You should also be able to handle displaying a button from within the same RowDataBound handler.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(condition)
{
Button btn = (Button)e.Row.FindControl("ButtonID");
btn.Visible = false;
}
}
}

Checkbox in DataGridView does not change its value after checking it

I have checkboxes in one of my columns in DataGridView.
And now I have a problem: When I click once on Checkbox it changes but only visualy, in code its value is still set to false. But if I click on Checkbox and then anywhere else on my datagridview (or change its value manually in code on true) it changes its value on true.
How can I force my checkbox to change value after one click? (it's annoying that checking checkbox actually does not check it).
Thanks for any help.
Changes to underlying data source are applied when the controls lose the focus.
You can handle it explicitly in CellContentClick event.
Please read the linked documentation thoroughly, as it describes similar scenarios, and discusses different type of grid cells.
Found also this. Exactly the same problem.
Lets assume like you have a form with name Form1
A DataGridView in it name dgv
A CheckBoxColumn in it with ColumnIndex = 5
public Form1()
{
InitializeComponent();
dgv.CellContentClick += dgv_CellContentClick;
}
Commit the change for datagrid within the event for that specific column
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5) dgv.EndEdit();
}

How to retrive a Control in a specific ROW in edit mode in a GridView

I have a GridView with many rows.
When a User click the EDIT button in GridView I need to retrieve a Control in that specific row (now in edit mode).
This Logic should work on GridEvent _RowUpdating
At the moment my code (wrong) look inside every Row, so the Control founded is not unique and I receive an error.
// Event handler
protected void uxManageSponsoredContentsDisplayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
// My code (Wrong!!!!):
foreach (GridViewRow row in uxManageSponsoredContentsDisplayer.Rows)
{
TextBox uxStartDate = (TextBox)row.FindControl("uxEffectiveStartDateInput");
}
Hope my question is clear. Any idea how to do it? Thanks
Solution:
TextBox uxStartDate = (TextBox)uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput");
You need to use the GridViewUpdateEventArgs e as it contains index of row being updated.
Use something like
uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput")

Categories