I have designed a gridview with a radio button inside the itemtemplet. I also have a confirm button. A user is allowed to check several radio button and when a confirm button is pressed then database is updated with the value of radio button.
But when i check the button it is shown as checked in UI but when I check through code behind, it shows checked property of radio button as false.
for (int i = 0; i < gvTransaction.Rows.Count; i++)
{
if (!((String.IsNullOrEmpty(gvTransaction.Rows[i].Cells[0].Text)) || (gvTransaction.Rows[i].Cells[0].Text == " ")))
{
string transactionID = gvTransaction.Rows[i].Cells[0].Text;
RadioButton btn = (RadioButton)gvTransaction.Rows[i].FindControl("rbtSelect");
if (btn.Checked)
{
// Although the radiobutton is checked code never reach here.
}
}
}
If this is Winforms, my guess would be because the edited Radio button cell value is not committed until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler like this:
void gvTransaction_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (gvTransaction.IsCurrentCellDirty)
{
gvTransaction.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Use this if the radiobutton.checkedchanged event.
How are you binding the GridView? If you are doing it in Page Load, make sure that you are not reloading the gridView on page PostBacks
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Bind GridView here }
do let us know if this help
Related
When I click on one radio button, it sets the text in my richtextbox. If I click on another one, it'll do nothing. Is it possible to replace the text with another radio button?
private void M_buttonComment_CheckedChanged(object sender, EventArgs e)
{
if (M_buttonComment.Checked) //If checked == true
{
// Set the text to be "Comment" //
M_TitleTextBox.Text = "Comment - ";
}
}
You need to subscribe to the same CheckChanged event for both the radio buttons.
Set this property for both radio buttons. (Name the method whatever you want, but make sure the name of the method is the same in the code.)
Then in your code:
private void SomeCustomEvent(object sender, EventArgs e)
{
if (radBtnOne.Checked) //If checked == true
{
M_TitleTextBox.Text = "From radio button one";
}
else if(radBtnTwo.Checked)
{
M_TitleTextBox.Text = "From radio button two";
}
}
Notice that the same thing is happening if either radio button is checked in my example. If you don't care which radio button was checked and just want to do the same thing regardless then the following would work. sender in this case would be the radio button clicked.
But you could also figure out which radio button was clicked by looking at their .Name property.
private void SomeCustomEvent(Object sender, EventArgs e) {
RadioButton rb = (RadioButton)sender;
if (rb.Checked) { // From either radio button
M_TitleTextBox.Text = "A radio button was clicked.";
if(rb.Name = "radBtnOne") // To check which one was checked.
{
// Now we know which radio button was clicked. Same process for the second
}
}
}
I have a FormView and when user is on InsertTemplate, I want the FormView to stay on the InsertTemplate after user clicked the Insert button if the data inside the InsertTemplate is not valid.
I have tried to use many events in FormView but none of them worked.
The closest event I can guess is FormView_ItemInserting()
protected void OfferDataFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
if (Page.IsValid == false)
{
e.Cancel = true;
}
}
But this code doesn't even get triggered after the Insert button was clicked. So I cannot stop the FV changing its template from InsertTemplate to EditTemplate.
I'm making a settings form, where user can assign custom hotkeys for the application. There's a TextBox, and by clicking it with mouse, it focuses and waits for one keypress and then defocuses (by focusing another label):
private void txtKey_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
private void txtKey_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Text = e.KeyCode.ToString();
label1.Focus();
}
Is there a way to defocus focused TextBox (and cancel the key assinging process), by either clicking it again with mouse, or by clicking the GroupBox around it? I can't figure out how to check if TextBox was already focused when clicked (because when clicked, it gets focused before I can test if it's focused). Of course I can add a button "Cancel" next to the TextBox, but that's not what I want.
There is no Click-event for GroupBox, so I can't defocus TextBox by clicking GroupBox around it. Or can I somehow?
You can set/remove the Focus with
Keyboard.Focus = null;
You can also register to the following event:
public event MouseButtonEventHandler PreviewMouseLeftButtonDown
This event fires every time you click on the TextBox, thus you can set the Focus there if you want to.
For Winforms there is a way as well. I'm not proficient in it, but here would be a way:
Make a textBox (e.g. named textBoxFocus) that lies outside your window. Size it 1, 1 and move it to -10,-10 for example. Then you can register to the Click event and write
textBoxFocus.Focus();
It's a bit of a roundabout way, but should achieve what you want.
Thanks to private_meta for getting me to right direction (in comments)! I set the flag with click event, and before setting the flag, testing if flag is set. So first click does not find the flag, but second will. And flag is cleared within textbox Enter-event (which fires before Click-event). Now every other click focuses and every other defocuses textbox, as I wanted.
private void txtKey_Enter(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Tag = null;
}
private void txtKey_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
if (textBox.Tag != null) label1.Focus();
textBox.Tag = "clicked";
}
One of the simple way is that, you may use a bool flag here.
Algorithm:
By default, the bool value is 0;
If(Textbox Selected && flag = 0)
Do your task; and flag = 1;
I hope I could satisfy your query and you can follow this algorithm.
I have two dropdowns, dependent upon each other selection. Now what I want is to reload both dropdown, when user clicks on first item, i.e, index 0.
onchange event is already associated with the dropdowns, so selectedindexchanged event is not being called.
What else can be done in this scenario?
'so selectedindexchanged event is not being called.'
In the properties for your combobox, set 'AutoPostBack' to true and this event will now be called.
Sorry; had to make an edit - I meant true not false.
Edit:
OnChange event is associated with my dropdown, and now am trying to add selectedindex changed event. Is it possible?
Yes, controls can have many events attached to it.
Code which works for selectedindex :
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 10; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.AutoPostBack = true;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
I have a Windows Forms app that displays a list of objects in a DataGridView.
This control renders bool values as checkboxes.
There's a set of three checkboxes in the object properties that are mutually exclusive. At most one of them can be true. Accordingly I'd like the checkboxes to act like a set of radio buttons.
Just a side remark from the old guy: I think people these days don't even know why these are called radio buttons. In the old days a radio in a car had 4 or 5 buttons, and depressing any one of them caused all the others to pop out. They were mutually exclusive. "Radio button" is probably not a helpful description these days because radios don't have buttons like that anymore, I don't think.
How can I do it? I figure if I attach a "CheckedChanged" event to the checkboxes, and I know the row, I will be able find all the other checkboxes.
What event can I hook to grab the checkbox control when it is first rendered, so that I Can attach the CheckedChanged event to it? I know about DataGridView.CellFormatting, but I think that's wrong because it gets called every time the DataGridView paints. I really need an event that is called only the first time the DGV is rendered.
Thanks to KeithS for the helpful answer.
When I looked in the doc for CellValueChanged, I found this helpful bit:
The DataGridView.CellValueChanged event occurs when the user-specified value is committed, which typically occurs when focus leaves the cell.
In the case of check box cells, however, you will typically want to handle the change immediately. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
This is the code I used to get the radio behavior:
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
// Manually raise the CellValueChanged event
// by calling the CommitEdit method.
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
// If a check box cell is clicked, this event handler sets the value
// of a few other checkboxes in the same row as the clicked cell.
if (e.RowIndex < 0) return; // row is sometimes negative?
int ix = e.ColumnIndex;
if (ix>=1 && ix<=3)
{
var row = dataGridView1.Rows[e.RowIndex];
DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell) row.Cells[ix];
bool isChecked = (Boolean)checkCell.Value;
if (isChecked)
{
// Only turn off other checkboxes if this one is ON.
// It's ok for all of them to be OFF simultaneously.
for (int i=1; i <= 3; i++)
{
if (i != ix)
{
((DataGridViewCheckBoxCell) row.Cells[i]).Value = false;
}
}
}
dataGridView1.Invalidate();
}
}
The one you want is CellContentClick, on the DGV itself. Attach a handler that checks to see if that column of the DGV is a CheckBoxCell, and if so, uncheck all other checkboxes on the row.
Just a note though, for a CheckBoxCell, this event fires before the checkbox value actually changes. This means that regardless of what you do to the current cell, it will be overridden by events that fire later. The behavior that will shake out of this is that you can have none of the cells on a row checked, by checking one box on the row and then checking it again (whether you try to set the checkbox value in the handler or not, the checkbox will end up cleared after the second click). To overcome that and force one of the checkboxes to be checked, you can handle CellValueChanged instead, and if the cell that changed is the current cell and is unchecked, check it.
private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
{
foreach (DataGridViewRow row in (sender as DataGridView).Rows)
{
if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
{
row.Cells[e.ColumnIndex].Value = false;
}
}
}
}
}
private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridViewClient.IsCurrentCellDirty)
{
dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
It would get too easy here:
Conisder your checkbox column is the 2nd column in your datagridview.
private void YourDatagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (IsHandleCreated)
{
if (YourDatagridview.CurrentCell == YourDatagridview.Rows[e.RowIndex].Cells[1])
{
if (Convert.ToBoolean(YourDatagridview.CurrentCell.Value) == true)
{
for (int i = 0; i < YourDatagridview.RowCount; i++)
{
if (YourDatagridview.Rows[i].Cells[1] != YourDatagridview.CurrentCell)
{
YourDatagridview.Rows[i].Cells[1].Value = false;
}
}
}
}
}
}
And call this too:
private void YourDatagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.YourDatagridview.IsCurrentCellDirty)
{
YourDatagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
and Voila!!