How to Change a Gridview Control? - c#

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.

Related

Showing Datetimepicker value into Datetimepicker box from database

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.

Changing text of an autogenerated select column of a gridview in asp.net - How?

I would like to change the text of the autogenerated "select" column in an ASP.NET GridView control. The text needs to be changed to the value of a DataField.
I suspect that there is a very logical way to do this but I am missing it.
I am able to add controls and data via the pre-render event but is there an easier better way?
Use the TemplateField and place into it buttons or linkbuttons with appropriate CommandName property: ButtonField.CommandName Property
You may set this button text using DataBinder.Eval method.
The easiest way I found to do this is after the calling DataBind() just before the gridview control is displayed.
foreach (GridViewRow row in gvAgreementList.Rows)
{
LinkButton lb = (LinkButton) row.Cells[0].Controls[0];
lb.Text = "Edit";
}
after <column> write this:
<asp:CommandField ShowSelectButton="True" SelectText="Save" />
and remove AutoGenerateSelectButton="True" from Gridview attribute.
First remove auto generated select then go to GridView tasks.. top right Button of GridView and then click on commandfields -> Select then edit SelectText.
(Edited answer of ShaileshK with some changes)
Go to GridVIew tasks.. top right Button of GridView and then click on edit columns
In selected fields section Click on select field. change the value of select text.
done.

How to display only the records that satisfy the condtion in a gridview using ASP.NET?

I am inserting a new record in a form that has two textboxes txtName, txtAge and I am using an ajax calender extender txtDOB and a gridview control gdvDisplayRecords with 3 fields name, dob and age. So when i select any date from ajax calender extender, the gridview must display only those records from the database which have same DOB before i try to add this new record. Please help me with the code as i am new to gridview control.
The quickest way would be filtering grid-view data source. For example, if grid-view is bound to a DataTable then you can create DataView with filter on DOB and bound grid to the view. If your data source is an array or list then use methods such as Array.FindAll or List.FindAll for filtering.

How to add a Textbox to Dynamic GridView

How do I add Textbox to a dynamic Gridview. Do I need to have a customised Templete Field ? Is there any other way ??
Do you mean you want to dynamically generate rows with TextBoxes in a GridView? If so, then there is a good explanation here:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx
Here's one if you're just looking to add a control to a single cell. It's a DropDownList, but could be modified to add a TextBox:
http://www.devasp.net/net/articles/display/708.html

Syncfusion GridList Control Column Header Click

I would like to order contents in a column when user clicks column header on the Syncfusion GridList control. Does anyone have any idea on how I might achieve this using C#?
I don't have them installed right now but it should be something like this:
GridControl.TableDescriptor.AllowSort = true;
This should make the columns sortable, thus when clicking on the column header it will order them.

Categories

Resources