how to show data related to selected checkbox in grid - asp.net

one dropdown list, from and to date and search button.
when user select value from dropdown list select date and press search button user can view data in grid according to the selected field till now i have completed my project now what i want is when i select a checkbox to show data related to particular column in the gridview in bulk suppose i have 50 user and 25 are from delhi city now i want when user select checkbox it can only view delhi user in the grid how can i do that..thanks in advance
protected void Chk_CheckedChanged(object sender, EventArgs e)
{
if (Chk.Checked!=null)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con;
if (ddlPortal2.SelectedIndex != 0)
{
con = new SqlConnection("Data Source=.;Initial Catalog=Bart;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("select * from Form3 where Portal='" + ddlPortal2.SelectedValue + "' and Query=Query", con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}

you need to add
OnCheckedChanged="EventHandler"
event on the checkbox and fill the Gridview inside the event

There are several options. If you don't mind a postback, then you can set autopostback for the control. Then you can handle the checked event on the server side, filter the dataset and redind the grid. If you would like to avoid a postback, you can use AJAX to do this. But this will be more complex and if you are already using an asp.net datagrid, the server-side rout is much more inline with the webforms methodology.

Related

ASP.Net GridView Edit/Update/Cancel/Delete Events On RowClick

We've got an ASP.Net application that contains a GridView Control that contains row edit functionality.
This allows a user to Edit, Delete, Or Cancel editing on a particular row.
For Example Read Only Mode Looks Like This:
And Edit Mode Looks Like this:
The mechanism that allows the user to enter Edit mode is based on an Edit Button in a template column that changes the selected row from a read only row to an editable row using a RowEditing event something like this:
protected void grdOfMine_RowEditing(object sender, GridViewEditEventArgs e)
{
grdOfMine.EditIndex = e.NewEditIndex;
ReBindDataGrid();
}
Canceling is pretty much the opposite where we have a button click event that changes the row back to ready only mode:
protected void grdOfMine_RowEditing(object sender, GridViewEditEventArgs e)
{
grdOfMine.EditIndex = -1;
ReBindDataGrid();
}
(Apologies to those who are already familiar with this aspect of ASP.Net forms development.)
We've also created a footer row that allows a user to add a new row:
We're looking for a way to extend the ASP.Net GridView control do this without using the buttons to fire the events.
For example:
Allow a user to enter edit mode for a row, by clicking in a cell of any given row and update the selected record say, on an Enter keyboard input event (Instead of the Edit Button).
Delete a record say, on a delete keyboard input event (Instead of the Delete Button).
Add a record in a similar fashion (Instead of the Add Button).
We were attempting this functionality using Infragistics controls, however we had a very tough time getting these to work, so we decided not to use them.
Thanks in advance
I am working on asp.net gridview on webforms and using Gridview RowCommand method of GridView OnRowCommand event for the Buttons View, Edit & Update inside TemplateField.
if (e.CommandName == "EditContract") {
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
gvContract.SelectRow(rowIndex);
using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#RoleName", SqlDbType.Char).Value = Session["RoleName"];
cmd.Parameters.Add("#UnitName", SqlDbType.Char).Value = Session["UnitName"];
cmd.Parameters.Add("#ContrSerialNo", SqlDbType.Int).Value = SerialNo;
dAdapter = new SqlDataAdapter(cmd);
DataTable DtContract = new DataTable();
dAdapter.Fill(DtContract);
}
if (e.CommandName == "UpdateContract") {
lblMessage.Text = "";
lblFile.Text = "";
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ContrNewRev", SqlDbType.VarChar).Value = ddNewOrRevised.SelectedValue;
cmd.Parameters.Add("#ContractTitle", SqlDbType.VarChar).Value = txtContractTitle.Text;
cmd.Parameters.Add("#FinancerID", SqlDbType.Int).Value = ddFinancer.SelectedValue;
}
This code is working fine when the page loads first time but has 2 problems after that. i.e. 1. It is editing and updating data when the page gets load for the first time but when I try to edit the same row or any other then it doesn't. I know it is because I have defined the !Page.IsPostback method on the Page_Load event but I do not know how to tackle this situation. 2. Problem is How to restrict the gridview row to update only that data whose row is selected?
Please suggest me a solution.
GridView Sample

Radtrelist binding

I am trying to bind the simple employee table to Radtreelist but it shoes only columns not data.
source view-->
following is code that i used to bind data to Radtreelist control
void binddata()
{
cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
strquery = "select Emp_Id,Emp_Name,Emp_Salary,DeptID from Employee";
da = new SqlDataAdapter(strquery, cn);
dt = new DataTable();
da.Fill(dt);
RadTreeList1.DataSource = dt;
RadTreeList1.DataBind();
}
and table having field Emp_Id,Emp_Name,Emp_Salary,DeptID
so when i run the application it shoes only column of database
You're probably not setting the DataKeyNames and ParentDataKeyNames properties of the RadTreeList
Check the demo here.

How to sync Datagridview and datasource when user sorts column?

I have a DataGridview in a windows form project that is directly bound to a Dataset through a SqlDataAdapter. I have one editable column which a user can edit and that works great if the user does not sort the column.
If the user sorts the column the underlying datasource gets out of sync with what the Datagridview is displaying.
What is a standard technique to keep the gridview and datasource in sync when a user sorts it?
Thanks!
As per my understanding auto sorting does not works with DataGridView, for performing sorting you need to sort the datasource and then rebind it to the GridView as shown in code below
private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Never use Queries like this always use Stored procedures
SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories", myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Categories");
DataView dv = new DataView(ds.Tables["Categories"]);
if ((numberDiv % 2) == 0)
dv.Sort = e.SortExpression + " " + "ASC";
else
dv.Sort = e.SortExpression + " " + "DESC";
numberDiv++;
myDataGrid.DataSource = dv;
myDataGrid.DataBind();
}
I can't seem to find an event that triggers for clicking on the header to sort a datagridview.

Edited value does not change while updating in website

My scenario is as follows.
I'm using ASP.NET 2.0 and VB.NET.
I have a login page. If I am an existing user, when I log in, my user details are popped in the respective textBoxes. Now I wish to change one of these fields. I edited one and then clicked on the Edit button to update the record. It's not updated but instead it brings the existing value from the text box.
How can I achieve this?
DBCmd.CommandText = "update IOMFNewMember set FirstName=#FirstName,MiddleName=#MiddleName,LastName=#LastNa,Gender=#Gender,DateofBirth=#DateofBirth,MartialStatus=#MartialStatus,DateofWedding=#DateofWedding,Nationality=#Nationality,ResidenceCountry=#ResidenceCountry,DateofJoiningIOMf=#DateofJoiningIOMf,EmailId=#EmailId,mobileno=#mobileno,AtPresent=#AtPresent,familykuwait=#familykuwait,DateofDeath =#DateofDeath where UserName=#Entery and MemberID=#MemberID"
For your requirement you have to run a sql command same as that u are using to populate the textboxes with the user details.
That is supposing you are using this in the login button click event
string strSql="select * from customers where id=" + txtLoginId.Text;
SqlConnection con=new SqlConnection(strCon);
con.Open();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=new SqlCommand(strSql,con);
DataSet dset=new DataSet();
da.Fill(dset);
con.Close();
You will have to use this in the edit button click event :
string strSql="update EmployeeTable set password = '" + txtPassword.Text + "'";
SqlConnection con=new SqlConnection(strCon);
con.Open();
SqlCommand cmd=new SqlCommand(strsql, con);
cmd.ExecuteScalar();
con.Close();
Hope this will clear you confusion.
This is just an example. For a real time situation its advisable to use stored procedures only for any database query.

Why SQLCommandBuider not DELETING?

In the following code UPDATE is working but DELETE is not working. When I DELETE row it delete it and GridView1 does not show it but database still has it. customers has PrimaryKey.
using (SqlConnection connection = new SqlConnection(conString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from customers", connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
connection.Open();
DataTable customers = new DataTable();
adapter.Fill(customers);
// code to modify data in DataTable here
customers.Rows.RemoveAt(rowIndex);
GridView1.EditIndex = -1;
GridView1.DataSource = customers;
GridView1.DataBind();
adapter.Update(customers);
You don't want to remove the row from the DataTable but delete it.
The DataRow's DataRowState must be modified to deleted, then the DataAdapter knows that it must delete it in the datasource.
Following will delete it:
customers.Rows(rowIndex).delete();//now DataRow's DataRowState is Deleted
adapter.Update(customers); // now it's actually deleted
Futher informations: MSDN How to: Delete Rows in a DataTable
By the way, following would prevent the DataRow to be deleted, if you've changed your mind:
customers.Rows(rowIndex).RejectChanges();
The RowState will change back to Unchanged.
This works either for the DataRow or for the whole DataTable.

Resources