Why does grid row change? - c#

I have a asp.net page with a grid and, above the grid, fields showing the contents of the highlighted row and some buttons (edit, add, delete, etc...).
If the user clicks the edit button then it enables the fields for editing.
However, after enabling the fields the grid is then refreshed or something and the first row is then highlighted and the fields on the screen changes. Why? How do I stop this?
Code is below:
protected void btnEdit_Click(object sender, EventArgs e)
{
ChangeButtons(null, EventArgs.Empty);
ChangeFields(null, EventArgs.Empty);
updatetype = "Edit";
}
protected void ChangeButtons(object sender, EventArgs e)
{
btnAdd.Enabled = !btnAdd.Enabled;
btnCopy.Enabled = !btnCopy.Enabled;
btnEdit.Enabled = !btnEdit.Enabled;
btnDelete.Enabled = !btnDelete.Enabled;
btnCancel.Enabled = !btnCancel.Enabled;
btnSave.Enabled = !btnSave.Enabled;
btnClose.Enabled = !btnClose.Enabled;
btnSearchCredits.Enabled = !btnSearchCredits.Enabled;
btnClearCredits.Enabled = !btnClearCredits.Enabled;
}
protected void ChangeFields(object sender, EventArgs e)
{
txtSrchCredible_ID.Enabled = !txtSrchCredible_ID.Enabled;
txtSrchADP_ID.Enabled = !txtSrchADP_ID.Enabled;
txtSrchStart_Date.Enabled = !txtSrchStart_Date.Enabled;
txtSrchEnd_Date.Enabled = !txtSrchEnd_Date.Enabled;
txtCredible_ID.Enabled = !txtCredible_ID.Enabled;
txtADP_ID.Enabled = !txtADP_ID.Enabled;
txtEmployee_ID.Enabled = !txtEmployee_ID.Enabled;
txtStart_Date.Enabled = !txtStart_Date.Enabled;
txtEnd_Date.Enabled = !txtEnd_Date.Enabled;
txtReason.Enabled = !txtReason.Enabled;
txtUnits.Enabled = !txtUnits.Enabled;
txtPercentage.Enabled = !txtPercentage.Enabled;
txtOther.Enabled = !txtOther.Enabled;
}

Related

How to cause a Control follow its DataSource?

At the beginning, when I assign my collection to a control's DataSource, it follows what is in the collection. During the execution of my program, when the collection changes, I don't see any change in the control's content. How can I make the control follow the latest changes of the collection?
What I have is:
private void FormEditImages_Load(object sender, EventArgs e)
{
radListView1.DataSource = WebServiceHelper.Instance.CurrentSession.Images;
radListView1.DisplayMember = "Name";
radListView1.ValueMember = "Id";
}
private void radListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
var cameraOverlayImage = e.Item.DataBoundItem as CameraOverlayImage;
e.Item.Image = CreateThumbnail(cameraOverlayImage.Image, e.ListViewElement.ItemSize.Width - 12, e.ListViewElement.ItemSize.Height - 16, e.ListViewElement.BackColor);
e.Item.TextAlignment = ContentAlignment.MiddleCenter;
e.Item.TextImageRelation = TextImageRelation.ImageAboveText;
e.Item.ImageAlignment = ContentAlignment.MiddleCenter;
}
How can I change the ItemDataBound method when I change Load to:
private void FormEditImages_Load(object sender, EventArgs e)
{
var bindingSource = new BindingSource(WebServiceHelper.Instance.CurrentSession.Images, "Name");
radListView1.DataSource = bindingSource;
}

How to make one event to identify whether multiple radio buttons are checked?

I have the following code which checks each radio button (Temp30, Temp40 and Temp60) and does the necessary things such as turning the wash temperature light on etc...
I want to create an event which handles all 3 radio buttons. I thought it could possibly have something to do with the groupbox they are in? (it is called TempGroupBox)
Any help would be much appreciated!
private void Temp30_CheckedChanged(object sender, EventArgs e)
{
if (Temp30.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._30degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp40_CheckedChanged(object sender, EventArgs e)
{
if (Temp40.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._40degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp60_CheckedChanged(object sender, EventArgs e)
{
if (Temp60.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._60degrees;
SpeedGroupBox.Enabled = true;
}
}
You can bind all radioButton's event to the same handler and use sender parameter to get the control that the action is for.
private void Temps_CheckedChanged(object sender, EventArgs e)
{
string checkedName = ((RadioButton)sender).Name;
if(checkedName == "Temp40")
{
...
}
else if(checkedName == "Temp60")
{
...
}
}
You can add event handler for all RadioBUttons's like that after InitializeComponent():
var radioButtons =this.Controls.OfType<RadioButton>();
foreach (RadioButton item in radioButtons)
{
item.CheckedChanged += Temps_CheckedChanged;
}

check box not working properly at page load

I have got problem with check-box to enable corresponding control next to it. My Requirement is at the page load we want to disable the all textboxes and dropdownlists by using checkbox
if the check-box is checked the control next to that check-box will be enabled for that i have done like this....
at page load
i have written like this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
}
and then in checkbox check changed event i have written like this
protected void ChckOrdType_CheckChanged(object sender, EventArgs e)
{
if (ChckOrdType.Checked)
ddlOrdType.Enabled = true;
else
ddlOrdType.Enabled = false;
}
protected void chkPlntPric_CheckChanged(object sender, EventArgs e)
{
if (ChkPlntPric.Checked)
ddlPlntPric.Enabled = true;
else
ddlPlntPric.Enabled = false;
}
protected void chkExcluBro_CheckChanged(object sender, EventArgs e)
{
if (ChkExcluBro.Checked)
ddlExcluBroker.Enabled = true;
else
ddlExcluBroker.Enabled = false;
}
but results is like this ...
I am getting checkbox not checked and control next to it is enabled...But this not what i want
My results is if the check-box is not checked the control next to it is disabled
would any one pls help on this....
Thanks In advance......
This is because you have just wrote ONLY to uncheck the checkboxes in the pageload and not to disable the controls behind the checkbox; If thats needed, then your snippet in the pageload should be:
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
ddlOrdType.Enabled = false;
ddlPlntPric.Enabled = false;
ddlExcluBroker.Enabled = false;
.........
}
or
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
ChckOrdType_CheckChanged(sender,e);
chkPlntPric_CheckChanged(sender,e);
chkExcluBro_CheckChanged(sender,e);
...
}
Disable text boxes in page load like below.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
DisableFirstTime();
......
.....
}
private void DisableFirstTime()
{
ddlOrdType.Enabled = false;
ddlPlntPric.Enabled = false;
ddlExcluBroker.Enabled = false;
}

C# UserControl Not and Yes with Text

The Default is No Checkbox
When I run the program and Click the Yes Checkbox the program overflowed
private void checkEdit1_Click(object sender, EventArgs e)
{
checkEdit2.Checked = false;
textEdit1.Enabled = true;
answered = true;
optional = textEdit1.Text;
if (!checkEdit1.Checked)
{
checkEdit1.Checked = true;
checkEdit2.Checked = false;
textEdit1.Enabled = true;
optional = textEdit1.Text;
}
}
private void checkEdit2_Click(object sender, EventArgs e)
{
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
if (!checkEdit2.Checked)
{
checkEdit2.Checked = true;
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
}
}
What you think is the error ?
Instead of the Click event you should use the CheckedChanged event in this way:
checkEdit1.CheckedChenged += new EventHandler(checkEdit1_CheckedChanged);
checkEdit2.CheckedChenged += new EventHandler(checkEdit2_CheckedChanged);
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
private void checkEdit2_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
But the best way in this case is to use a group of radio buttons.
Assuming that those methods are wired up to checkEdit1 and checkEdit2 I would advise that you don't make a change to checkEdit1 in checkEdit1_Click as it has already changed - only modify the state of the alternate.
However, when you modify the state of the other, unless you're careful, you're going to get called back. Eventually the computer gives up -- the overflow!
As mentioned in a comment by #Cyborgx37, radio buttons are the better UX choice here!
A possible solution, bind a single method to the OnClick to BOTH checkboxes:
private bool internallyUpdating = false;
private void CheckboxClick(object sender, EventArgs e)
{
if ( !internallyUpdating )
{
// Prevent subsequent changes
internallyUpdating = true;
// Exchange 'checked' state
if ( sender == checkEdit1 )
{
checkEdit2.Checked = !checkEdit2.Checked;
}
else // if (sender == checkEdit2)
{
checkEdit1.Checked = !checkEdit1.Checked;
}
// other logic here..
// restore 'on change' functionality.
internallyUpdating = false;
}

Blank row is added to my DataGridView when working with a binding list

I have a DataGridView that I can add test data to my data grid view but when I want to enter anything manually (through textboxes and drop down boxes), I just get a blank row added to my data grid view
This is the code I have: This is to get to the form where data is entered manually
private void button1_Click(object sender, EventArgs e)
{
frmAddReptile frmAddReptile = new frmAddReptile();
frmAddReptile.ShowDialog();
_ReptileList.Add(_Reptile);
}
Then, on that form I have
internal Reptile _Reptile;
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
this.Close();
}
Then, once the button is clicked only a black row added to my data grid view.
Ok i have now figured out if i add everything to the samewindows form it works(adds to the data grid view)
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
_ReptileList.Add(_Reptile);
}
this is what i have now but how can i make it add data form a differnt form
does it have something to do with internal Reptile _Reptile;?

Categories

Resources