I have a DataGridView. I want its 1st column or any desired column (which has textboxes in it) to be NUMERIC ONLY. I am currently using this code:
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
This code works but the problem is that all the textboxes in all the columns are getting numeric only.
I figured it out myself :)
Just removed the previous events in the starting of the function which resolved my issue.
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
You can add in a List all the numeric cells index.
Like this:
List<int> list_numeric_columns = new List<int>{ dataGridViewItems.Columns["itemID"].Index};
You add all the columns you need there.
Then, instead of doing this:
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
You do this:
if (list_numeric_columns.Contains(dataGridViewItems.CurrentCell.ColumnIndex))
That should work. You will have to add the columns just once..
Hope thats helps
This is how to use EditingControlShowing with TextBox → Keypress Event
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var itemID = e.Control as TextBox;
if (dataGridViewItems.CurrentCell.ColumnIndex == 1) //Where the ColumnIndex of your "itemID"
{
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
itemID.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
e.Handled = true;
}
Click the textbox that you want to be numeric only then in the properties click the "events" (looks like thunder) then look for the "keypress" then double click then type this:
if (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
Related
i'm just new to c# and visual studio
so i figured how to mask the password in my datagridview with this
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 5 && e.Value != null)
{
dataGridView.Rows[e.RowIndex].Tag = e.Value;
e.Value = new String('\u25CF', e.Value.ToString().Length);
}
}
Now i wanted to show the password again when i click on the cell, i figured it would be on dataGridView_CellClick event but i can't figure how to make it show up again. do i assign it to e.Value again?
use a textbox by design in that column of gridview and from source add a checkbox too !
then use this code
private void FromSourceAddedCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (FromSourceAddedCheckBox.Checked == true)
{
GridviewTextboxID.UseSystemPasswordChar = true;
}
else
{
GridviewTextboxID.UseSystemPasswordChar = false;
}
}
I want to add a new row in the grid when i press a enter key in a texbox in the gridview
private void dgReceipt_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgReceipt.CurrentCell.ColumnIndex == 5)
{
DataGridViewTextBoxEditingControl txtcontrol = e.Control as DataGridViewTextBoxEditingControl;
if (txtcontrol != null)
{
txtcontrol.KeyPress += new KeyPressEventHandler(txtcontrol_KeyPress);
}
}
}
private void txtcontrol_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
dgReceipt.Rows.Add(1);
}
}
I'm trying to validate any textbox in any DataGridView if it already contains .
I got this code which gives me InvalidCastException.
Any idea how to get around?
if (((DataGridTextBox)sender).Text.Contains(".") & e.KeyChar == '.')
{
e.Handled = true;
}
Most likely your "sender" isn't DataGridTextBox type. You can check it with code below:
DataGridTextBox dataGridTextBox = sender as DataGridTextBox;
if (dataGridTextBox != null)
{
//It's DataGridTextBox
}
else
{
//It isn't DataGridTextBox
}
So, you should to know type of your "sender", to do validation.
I resolved it with "EditingConrolShowing" event added for the DataGridView and folloing code:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) //This is to allow nubers with one dot as decimal place only in all datagrids
{
e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
}
}
private void Column1_KeyPress(object sender, KeyPressEventArgs e) //This is to allow nubers with one dot as decimal place only in all datagrids
{
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back & e.KeyChar != '.')
{
e.Handled = true;
}
if (((TextBox)sender).Text.Contains(".") & e.KeyChar == '.')
{
e.Handled = true;
}
}
i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .
As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.
can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
dataGridView1_CurrentCellDirtyStateChanged(sender, e);
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
these below links helped me to understand the concept of cellvalue_changed and cell_content_click..
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
and by the help of these links i finally got the solution to my problem
it is as simple as this
//replace the row number and column name with your own
if ((bool)dataGridView1.Rows[0].Cells["Column1"].Value)
{
//do your work
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
label1.Text = dataGridView1.Rows[e.RowIndex].Cells["Col1"].Value.ToString();
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == *someIndex*)
{
DataGridViewCheckBoxCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (cell != null)
{
if (cell.EditingCellValueChanged)
{
//CheckBox has been clicked
}
//here how to get the checkBoxCell value
var cellChecked = cell.EditingCellFormattedValue;
}
}
}
I would like to display a column in a datagridview as a column which contains password chars.I cannot figure it out why does this event is not triggered by the datagridview.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex == 3)
{
if(e.Value != null)
{
e.Value = new string('*', e.Value.ToString().Length);
}
}
}
Help please.
You can handle the EditingControlShowing event and then cast the editing control to a TextBox and manually set the UseSystemPasswordChar to true.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if(e.ColumnIndex == 3)//select target column
{
TextBox textBox = e.Control as TextBox;
if (textBox != null)
{
textBox.UseSystemPasswordChar = true;
}
}
}