Showing Datetimepicker value into Datetimepicker box from database - c#

Hello i have database called tbl_data, that have name,address, and 'date' type of data
example : 12/5/2021 (the data saved like this in my db)
I use datetimepicker toolbox to insert date into database,
now i have 1 data of a date inside my database
I try to show the data into Datetimepicker box, the idea is when I click 1 of data from Datagridview, its show 1 set of Data (name,date,address)
my question is
with this code I already success showing the name and address(which is a string), how can I show the date into this datetimepicker toolbox?
thank you
private void dgvData_DoubleClick(object sender, EventArgs e)
{
if(dgvData.CurrentRow.Index != -1)
{
Id = Convert.ToInt32(dgvData.CurrentRow.Cells[0].Value.ToString());
txtName.Text = dgvData.CurrentRow.Cells[1].Value.ToString();
txtAddress.Text = dgvData.CurrentRow.Cells[2].Value.ToString();
//dateTimePicker1.Text = dgvData.CurrentRow.Cells[3].Value.ToString();
// yea this code doesnt work because it convert to string but the toolbox was was not textbox but datetimepicker
}
}

You can simplify this if the grid uses a DataSource. Assuming that tbl_data is used as a DataSource to the grid, then instead of “manually” changing the data in the text boxes and date time picker, when the grid row is double clicked… you could set each text box and the date time picker's DataBindings property.
This will change the text in the text boxes and date time picker automatically when the user selects a different row in the grid... and your code will no longer need to "manually" set the values as your current code does. Something like…
txtName.DataBindings.Add("Text", tbl_data, "Name");
txtAddress.DataBindings.Add("Text", tbl_data, "Address");
dateTimePicker1.DataBindings.Add("Text", tbl_data, "Date");
Where, “Name” is the name of the “Name” column in tbl_data and the same applies to the “Address” column name and “Date” column name.
In addition, if the user changes the text in one of the text boxes or changes the date time picker value… then the grid will automatically reflect those changes.

Related

How to set and get the value of Data Bind Gridview Combobox Column in C#

I have a data grid view with combo box column in it, i have bind that column to datatable like this:
((grdItems.Columns[1]) as DataGridViewComboBoxColumn).DataSource = dt;
((grdItems.Columns[1]) as DataGridViewComboBoxColumn).DisplayMember = "LocationName";
((grdItems.Columns[1]) as DataGridViewComboBoxColumn).ValueMember = "Id";
now i can get the selected value like this:
var value = grdItems.Rows[0][1].Value;
but the issue is, when i manually add the rows in the gridview (without binding the grid), i cant get the value of the combobox cell.
i am adding rows like this:
grdItems.Rows.Add(1, 1, "Some Value");
when i use
var value = grdItems.Rows[0][1].Value;
this method to get the value, i returns me the text of the cell not the value i.e 1 in this case.
How can i solve this issue? since i want the value of the cell in case of adding rows manually, as well as data-bind rows.
Am assuming that by manually adding rows you mean from the UI , so one way to get the value of the newly added row is to use an event handler for the events raised when you add an row. You may use one of the following
1) RowsAdded https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded(v=vs.110).aspx 2) RowPrePaint - https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowprepaint(v=vs.110).aspx - not used this much but is also an option
this.myGridView.RowsAdded += new DataGridViewRowsAddedEventHandler(myGridView_RowsAdded);
private void myGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
var newRow = this.myGridView.Rows[e.RowIndex];
}
Edit: Based on the actual requirement stated in the comments - actual requirement being retrieving the newly added row's cell value (Id) corresponding to the combox box column on button save clicked event
Assuming you have bound the combo box to a data source itself , meaning, your
combobox.DataSource=someList
so what you can do is write a linq query
someList.Where( v=>v.Text.Equals("row.Cells[1].Value")).FirstOrDefault().Id

Clear selected Date in Radcalender

I am writing an application which updates the date selected in RadCalender to a database column on click of a button.
After the button is clicked, i want to clear the selected date so that the end user selects a date before clicking the button again
I have tired using the below code, but it dosen't appear to work
calExpirydate.SelectedDates.Clear();
You can use the following script:
var selectedDates = get_selectedDates(); //Gets the "list" of selected dates
unselectDates(selectedDates); // unselects all dates that may have been selected.
Code taken from: Telerik Reset Calendar Selected Date
----------------------------
radCal1.RemoveFocusedDate(true); //Will remove the calendar date currently selected.

DateTimePicker breaking data bindings

I have a form with two data-bound text boxes, one data-bound DateTimePicker and a BindingNavigator. I bind these components at run-time to the relevant data sources (the 'Text' and 'Value' of the Text Boxes and DateTimePicker respectfully are bound the relevant fields in a data set). All the components display the present records correctly. Normally, when one clicks the "Add New Item" button on the navigator, all the Text Boxes will clear in preparation for the new entry. However, I have the problem that this does not occur when the DateTimePicker is bound, and furthermore any attempts to update the datasets do not occur correctly. If I neglect to bind the DateTimePicker then all works as expected. What is the cause of this behaviour and is there a way around it?
The DateTimePicker doesn't support binding to nullable data. When you add a new row, the value for that field will be DBNull.Value by default and that cannot be converted successfully to a value that can be assigned to the Value property of the DateTimePicker, which is type DateTime. You need to either extend the DateTimePicker and add a new property that supports both DateTime values and DBNull objects and bind that or else set the DefaultValue property of your DataColumn so that there will always be a DateTime to display in the DateTimePicker.
This problem was resolved for my scenario by simply controlling the addrow method of the binding source based on this site: http://www.vbforums.com/showthread.php?645401-RESOLVED-datetimepicker-with-binding-source
Add a new button on the binding navigator (to use as your 'Add') and remove the old one.
Connect to the click event and set the datetime of the new row being added:
DataRowView row = (DataRowView)bs_.AddNew();
row["dbDateTime"] = DateTime.Now;
where dbDateTime is your database field.
Although you may have to add some more code to the click event, such as moving to the newly added row on the datagrid, this option was by far the easiest to implement.

How to Change a Gridview Control?

I have a C# ASP.NET project with a gridview. The gridview has a dataset as a datasource.
The grid includes a date field, but when you edit that field, you have to type in the date, and can't select a date since its a textbox.
How will you go about changing the control of a gridview.
This is what I've tried but it didn't work. (Just tried to convert all the textboxes to a calender as a test.
foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells)
{
cell.Controls[0] = cell.Controls[0] as Calendar; //This didnt work....
//do other stuff
}
Instead of doing that u can simply use calender exterder of ajax tool kit.here is the lnik for more details.Link
change the date column to template fiels. In edit template add a calender extender to the textbox. Simple.

How to set cell values programatically in a GridView?

Work on Asp.Net C# VS08.
I have a GridView which was binded to an ObjectDataSource with edit command link capabilities enabled.
Whenever the user go on edit a row and changes a given cell, I'd like to change another cell value programatically change a cell value (according to the new cell value typed by the user).
So, is it possible to change cell values programatically on edit mode?
I took a gridview with Five columns. In first column i have edit buttons. In second column i have name and in third i have age,StartTime,EndTime.On my name cell i used PopupControlExtender,When user click on Name Cell they see a gridview popup containing Name And Age from there they chose appropriate name.After chose the name i want Age will be set Automatically on My Age cell.
Where to Write,How to write methods.On PopupControlExtender popup grid i can select and set value on name cell using the bellow code
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
string b = ((Label)GridView2.SelectedRow.FindControl("lblCOM_NAME")).Text;
AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup( this.Page).Commit(b);
}
i want user click the name cell value after this age will be set automatically on Age cell from popup grid.How to set the Value?
Use a template column and then
row[0].Cell[0].FindControl("txtTextBox")
with the correct cast.
I dont have VS on this machine but that should be about right.

Categories

Resources