I navigated to a page where I want to be able to see the date i got from the Database and to edit that date if it is needed. When I change the date, it always go back to the date that was set by the database previously. How do I solve this. Below are my code:
private void dateData_Loaded(object sender, RoutedEventArgs e)
{
dateData.Value = DateTime.Parse(NavigationContext.QueryString["Date"]);
}
private void dateData_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
dateData.Value = (DateTime)dateData.Value;
}
I Think the loaded event is getting called every time you are changing the date and you are assigning the date you are getting from your querystring parameters. So try something like this And check if it works
private void dateData_Loaded(object sender, RoutedEventArgs e)
{
if(NavigationContext.QueryString.ContainsKey("Date"))
{
dateData.Value = DateTime.Parse(NavigationContext.QueryString["Date"]);
NavigationContext.QueryString.Remove("Date");
}
}
Related
i have a windows forms app which i want a certain button to change to enable if the textbox is not empty i tried to compare it to string.Empty but it wont work so i decided to compare to TextLength wont work either ..
Code down below:
private void Form1_Activated(object sender, EventArgs e)
{
if (firstDisplayTxtBox.TextLength > 0)
{
plusButton.Enabled = true;
}
}
mayble i place the if statement in the wrong method Let me know where i am wrong Big Thanks for helpers
Probably, you want to use TextChanged event, which fires every time when text in your firstDisplayTxtBox is changed.
public Form1()
{
InitializeComponent();
firstDisplayTxtBox.TextChanged += OnTextChange;
}
private void OnTextChange(object sender, EventArgs e)
{
plusButton.Enabled = firstDisplayTxtBox.Text.Length > 0
}
or
I have a WindowsForms app with a DataGridView bound to a SQL database. All columns except one ("ts_last_edit") are editable.
I want to update the "ts_last_edit" column only programmatically with the current DateTime every time someone changes some of the other columns AND saves the changes.
I tried this:
...
this.MyTableAdapter.Adapter.RowUpdating += Adapter_RowUpdating;
...
private void Adapter_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
e.Row["ts_last_edit"]=DateTime.Now;
}
But it did not work because e.Row is a read-only property apparently.
I tried changing the UPDATE SQL command and it works:
private void Adapter_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
// pseudo code
string newSqlCommand= "STUFF CURRENT DATE AND TIME IN COLUMN ts_last_edit";
e.Command.CommandText = newSqlCommand;
}
This works OK, but I was wondering if there is a better way to do it.
Thank you
you need to merge changes back to dataset after updating the time
private void brandBookBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
var changes = dataSet1.GetChanges();
if(changes!=null && changes.Tables["BrandBook"].Rows.Count>0)
{
foreach(row in changes.Tables["BrandBook"].Rows)
{
row["ts_last_edit"] = DateTime.Now;
}
}
dataSet1.Merge(changes, false,System.Data.MissingSchemaAction.Add);
this.brandBookBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.dataSet1);
}
I have a DataGridView in a form that uses a Backgroundworker to load the data and a pleasewait form to keep the UI responsive and show a gif so the user can see the item is still working.
After it is complete I need to somehow refresh the DataGridView with the new data. If i manually click on the headers to sort my ASC or DESC then it shows the up to date data. What or how is the best way to get this grid to refresh please?
public void btnSearch_Click(object sender, EventArgs e)
{
pleaseWait.Show();
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.TestTableTableAdapter.Fill(this.TestTableData.TestTableTable, txtHotName.Text, ((System.DateTime)(System.Convert.ChangeType(txtDepartFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtDepartTo.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
}
The above does what it needs to which is great. I just need to some how refresh without having to make the end user re-order a column to actually show refreshed data.
Following on from jdweng's comment of ;
The re-paint method doesn't automatically get called when adding data
to a DGV. So the trick is to set the datagridview1.DataSource = null,
and then back to the actual data source.
This fixed the option for me. My gif still spins and the UI updates as my criteria has changed.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
this.TestTableTableDataGridView.DataSource = null;
this.TestTableTableDataGridView.DataSource = TestTableTableBindingSource;
}
I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}
I Am using a DateTime Picker in WindowsForm Application of C#. I initalize My dateTImePicker with DateTime.Min Value. What I want is that when a user clicks that dropdown It Should Change its value to DateTime.Now Value and show the current value in calendar but I have to first open it then close it and then reopen it to get the required date in calendar. How can I Do it in a single Click? Any Help will be appreciated.
So far I have tried these events:
private void GroupEndingDate_MouseUp(object sender, MouseEventArgs e)
{
GroupEndingDate.Value = DateTime.Now;
}
private void GroupStartingDate_MouseDown(object sender, MouseEventArgs e)
{
GroupStartingDate.Value = DateTime.Now;
}
private void GroupStartingDate_DropDown(object sender, EventArgs e)
{
GroupStartingDate.Value = DateTime.Now;
}
Try this in your dropdown event. This worked for me.
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now.AddDays(-3);
}
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
}
The following works just fine for me without having to click the control a second time.
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
}
But I agree this approach seems a little odd to me. The date will be set every time the control opens up. Why not just initialize it to DateTime.Now in your form's Load event?