how to update gridview based on dropdownlist item - c#

I am creating a web page that contains one Dropdownlist and Gridview.
Query is Dropdownlist will contains SQL Server database table list. When I select a table name from dropdownlist the Gridview needs to show entire table data and able to perform edit, update, delete, cancel actions.
When I click edit, Gridview need to show Update and Cancel buttons and it update should update dropdownlist table and also delete.
Please any one can help.
Thanks in advance.

You have to rebind your grid view on selected index changed of dropdown
like this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
gridview.DataSource = dataSource;
gridview.DataBind();
}
where datasource is your database query result

You must hook in changing dropdoenlist and then update your grid
protected void YourDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
YouGridview.DataSource = YourdataSource;
YouGridview.DataBind();
}

Related

All the values from database on page load in dropdownlist to be bind and default value to be selected which can be chaged after updation

I have a dropdownlist which is bind with database, having some values.
It is properly retrieving all the values from the database, but it needs to be updated as well on button click and after updating it should select that value, my stored procedure is working perfectly
but I don't know how to set the updated value as the default value of the dropdownlist.
I am using C#, ASP.NET 4.0 and sql server 2008.
On pageload use a select query
then check the row count
example :
if (dtCheck.Rows.Count > 0)<br>
{
if (!IsPostBack)<br>
{
ddlAnswers.SelectedValue = dtCheck.Rows[0] ["columnname"].ToString()
}
}
Note; if(!IsPostBack) is used so that if you update the database using client side it will not retrieve the selected value in the drop down during page load
Store the selected dropdown selected value in Viewstate before button click event happens.
ViewState["selectedValue"] = ddlAnswers.SelectedValue;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["selectedValue"] !=null)
{
ddlAnswers.SelectedValue = ViewState["selectedValue"].ToString()
}
}
Please try this. This way you can reselect the preselected value.

Edit command from Gridview to Formview

I've got a Gridview that lists some SQL data.
And i've got a Formview setup with edit template.
I then want a LinkButton in Gridview, to open edit in formview, with the selected entry in gridview.
I've got this to capture my edit command from Gridview, but how i then trigger my Formview i dont know?
public void newsEdit_Command(Object sender, CommandEventArgs e) {
//Trigger formview edit from here...
//e.CommandArgument contains my ID of the selected row in gridview.
}
In the way you want, i think it is not possible.
You can do it in a simple way as:
1) Show Data in a grid view, When a user selects a row in the grid view, show that row data in the form view ( or you can use a details view )
2) After displaying data in form view, Now edit it's row.

Searching in C# Gridview

I have a grid view which display all of my saved data. When I click on date search button, data bind in grid view of particular date that I typed in text box. But when I try to go next page by clicking grid view pager button, grid view date bind all page of saved date. It's not binding data of that particular date. Please help me...
you can implement PageIndexChanging as below.
protected void GridViewExtUsers1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Set the index of the new display page.
GridView1.PageIndex = e.NewPageIndex;
// Rebind the GridView control
// if on search mode bind search results
// otherwise bind all data.
// implement your logic in different method
BindGridView();
}

C# gridview row onclick

I notice there is a "AutoGenerateSelectionButton" function but it's not really what I want. I want to be able to two things when a row is clicked (anywhere of the row):
change the color of that entire row.
get the value of a specific column and update another table accordingly.
How can I acheive that without writing client-side javascript functions?
Assuming this is a webform, you need to access SelectedIndexChanged event of the gridview using a codebehind file.
from here you can modify properties
protected void ChangedRow(object sender, EventArgs e)
{
this.GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
....
}

Event to bind a gridview inside another gridview

I have a gridview control inside another gridview control. When I click on the edit button the second gridview inside the 1st one should bind with a data source.
I am using a SqlDataSource and configured it. In which event of the gridview do I need to write the code for binding the records?
I am new to .net.
You can use RowCommand Event
protected void SecondGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
FirstGrid.DataBind()
}
}
You need to write your data binding logic in Parent grid's OnEditCommand event. Further details can be found here

Categories