Mutliple dropdowns and values on the Paging - asp.net

I have a GridView and I populate it via a DATASET . Three of its columns are three DropDownLists and AllowPaging is set to true for the grid view. My problem is when I choose a value on anyone of the ddl and click on serach button i get the data on the gridview which will have paginig, but when i click on the second page i loose teh filtered calue onthe drop down and i again get the earlier dataset.
Is there any way/idea to persist the selected values? Thanks for your help.
Please can you help me with this. if i am not filtering anyone of teh dropdown and then clicking on the second or third page i get the relevant data of that particular page. teh only problem is when i have a value selected on the dropdown .
code:
button click:
{
string _strBU = BUDropDownList.SelectedValue;
string _strOU = OUDropDownList.SelectedValue;
string _strPortalID = !string.IsNullOrEmpty(TxtEmpPortalID.Text.Trim()) ? TxtEmpPortalID.Text.Trim() : string.Empty;
string _strRU = RUDropDownList.SelectedValue;
string _strMngrPortalID = System.Web.HttpContext.Current.User.Identity.Name.ToString();
_strMngrPortalID = _strMngrPortalID.Substring(4, 6);
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "server=;uid=;pwd=;database=HROrgchartDB";
sqlConnection.Open();
SqlCommand sqlEmployeeDetailsCommand = new SqlCommand();
sqlEmployeeDetailsCommand.Connection = sqlConnection;
sqlEmployeeDetailsCommand.CommandText = "EmployeeSearch";
sqlEmployeeDetailsCommand.CommandType = CommandType.StoredProcedure;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#BU", SqlDbType.VarChar, 50)).Value = _strBU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#OU", SqlDbType.VarChar, 50)).Value = _strOU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#PORTALID", SqlDbType.VarChar, 6)).Value = _strPortalID;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#RU", SqlDbType.VarChar, 50)).Value = _strRU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#ManagerPortalID", SqlDbType.VarChar, 6)).Value = _strMngrPortalID;
SqlDataAdapter da = new SqlDataAdapter(sqlEmployeeDetailsCommand);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null)
{
// gvAddorRelease.Visible = true;
gridReportees.DataSource = ds;
Cache["D2"] = ds;
gridReportees.PageIndex = 0;
gridReportees.DataBind();
}
else
{
}
sqlConnection.Close();
}
}
event for paging :
gridReportees_PageIndexChanging:
{
gridReportees.PageIndex = e.NewPageIndex;
DataSet ds = new DataSet();
ds=(DataSet)Cache["D2"];
gridReportees.DataSource= ds;
gridReportees.DataBind();
}

Problem:
When you click on the next page, dropdown also rebinds due to which there selected index changed to 0.
Solution:
When you press the search button at that time you have to save the selectedvalues of the 3 dropdownlists. the dropdowns are present inside your gridview,so first you have to get the gridview row index. You can get the row index from gridveiw_selectedindexchange event. [Hint]
ViewState["svalue1"] = ((DropDownList) gv.Rows[index].FindControl("dropdownlistID")).Text;
// also get the selected values of other 2 dropdowns
Now after getting the selectedvalues you have to set the dropdwonlist.selected value to the values which we have saved in the ViewState.
gridReportees_PageIndexChanging()
{
// After binding the grid
// in this metho set the dropdown seleted value
// example get a reference of your dropdown
DropDownList ddl1 = (DropDownList) gv.Rows[index].FindControl("dropdownlistID");
ddl.SelectedValue = ViewState["svalue1"].ToString();
// follow the same steps for other 2 dropdownlists
}
Hope that it works for you.

Related

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.

RowClick and find does not work in Devexpress XtraGrid Sub Levels

I have populated a XtraGrid view with two leves.I can work with main level fine but when I want to find in second level by code or even I add an Event like OnRowClick or sth like that for sublevels it does not work.Does any one has my problems?
for example the name of gridview in sublevel is GridProperty..this code does not work
[C#]
GridProperty.ApplyFindFilter(bar_edit_search.EditValue.ToString());
or even events does not fire
[C#]
private void GridProperty_RowClick(object sender, RowClickEventArgs e){}
Here is how I populate my gridview
string owner = "SELECT [OBJECTID],[Name] ,[Family] ,[Father] ,[shenasname],[Dftarche] ,[Birthday] ,[education] ,[home_address] ,[farm_address] ,[ensurance] ,[phone] ,[home_number] ,[owner_id],[sodor] ,[shahr],[bakhsh] ,[dehestan] ,[rosta] ,[kodPosti] FROM [dbo].[OWNER]";
string strConnString = Properties.Settings.Default.land_gisConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand command = new SqlCommand(owner, con);
SqlDataAdapter adapter = new SqlDataAdapter();
dsMain = new System.Data.DataSet();
adapter.SelectCommand = command;
adapter.Fill(dsMain, "First Table");
dsMain.Tables.Add(iFeatureSet.DataTable.Copy());
adapter.Dispose();
command.Dispose();
DataRelation newRelation = new DataRelation("املاک شخصی", dsMain.Tables["First Table"].Columns["owner_id"], dsMain.Tables["Table1"].Columns["owner_ID"]);
dsMain.Relations.Add(newRelation);
}
GridAttrebuteTable.DataSource = dsMain.Tables[0];
GridAttrebuteTable.ForceInitialize();
GridOwners.Columns["shahr"].Visible = false;
GridOwners.Columns["bakhsh"].Visible = false;
GridOwners.Columns["dehestan"].Visible = false;
GridOwners.Columns["rosta"].Visible = false;
GridOwners.Columns["kodPosti"].Visible = false;
GridOwners.Columns["sodor"].Visible = false;
GridOwners.Columns["sodor"].OptionsColumn.AllowShowHide = false;
GridOwners.Columns["shahr"].OptionsColumn.AllowShowHide = false;
GridOwners.Columns["bakhsh"].OptionsColumn.AllowShowHide = false;
GridOwners.Columns["dehestan"].OptionsColumn.AllowShowHide = false;
GridOwners.Columns["rosta"].OptionsColumn.AllowShowHide = false;
GridOwners.Columns["kodPosti"].OptionsColumn.AllowShowHide = false;
SplashScreenManager.CloseForm();
There are no problems with sublevels. You must check the following:
0. For ColumnView.ApplyFindFilter method check the ColumnView.OptionsFind.FindFilterColumns property. According to documentation:
If the FindFilterColumns property is set to "*", the Find Panel
performs searches against all visible columns.
To explicitly specify search columns, set the FindFilterColumns
property to a string consisting of the corresponding field names
delimited by a semicolon (without space characters):
"Name;Value;Description".
So, if your FindFilterColumn property is set to wrong value then ApplyFindFilter method will not work.
1. For GridView.RowClick event according to documentation:
The RowClick event will not fire when clicking on a row cell, if data
editing is enabled and the ColumnViewOptionsBehavior.EditorShowMode
property is set to MouseDown (and to Default if multiple row selection
is disabled).
So, check your ColumnView.OptionsBehavior.EditorShowMode property and set it to EditorShowMode.Click.
2. Check the GridLevelNode.RelationName. It must be equal to RelationName in your underlying DataSource:
DataRelation newRelation = new DataRelation("املاک شخصی", dsMain.Tables["First Table"].Columns["owner_id"], dsMain.Tables["Table1"].Columns["owner_ID"]);
//...
gridControl.LevelTree.Nodes[0].RelationName = "املاک شخصی";

dropdownList doesn't displays selected value

I am new at asp.net. I have a dropdownList which displays all categories for a specific article. The problem is that when I want to edit an article it doesn't displays the value that exists as default selected in dropdownList.
protected void datalist2_OnItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
SqlConnection con = new SqlConnection(connection);
string Qry = "select * from kategoria";
SqlDataAdapter da = new SqlDataAdapter(Qry, con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds);
drpdKategoria.DataSource = ds;
drpdKategoria.DataValueField = "id";
drpdKategoria.DataTextField = "emertimi";
drpdKategoria.DataBind();
drpdKategoria.SelectedValue = drpdKategoria.Items.FindByText(Qry).Value;
//drpdKategoria.Items.FindByValue(string val).Selected = true;
con.Close();
con.Dispose();
ds.Dispose();
da.Dispose();
}
}
EditArtikull.aspx
<asp:Label ID="Label5" runat="server" style="font-weight: 700">Kategoria</asp:Label>
<fieldset>
<asp:DropDownList ID="drpdKategoria" runat="server" AutoPostBack="false"></asp:DropDownList>
</fieldset>
<br/>
ERROR:SystemNullReference Exception {"Object reference not set to an instance of an object."}
This line probably isn't going to find anything:
drpdKategoria.Items.FindByText(Qry)
Since Qry is a SQL statement:
string Qry = "select * from kategoria";
And I'm assuming that the display values in your DropDownList aren't SQL queries. Thus, when you call .Value on that first line, you're trying to de-reference something that isn't found (which is null), hence the error.
What item are you actually trying to find? If you want to select a default item, you need to be able to identify that item in some way. In your data that would be either by a known id value or a known emertimi value. For example, if you have a known emertimi value, it would be:
drpdKategoria.SelectedValue = drpdKategoria.Items.FindByText("some known value").Value
To make this a little more robust, you probably want to add some null checking. Something like this:
var defaultValue = drpdKategoria.Items.FindByText("some known value");
if (defaultValue != null)
drpdKategoria.SelectedValue = defaultValue.Value

Cast error on GridView Sorting event

I'm intercepting the RowDatabound event and using:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to get access to the unbound data source columns. It works fine on page load and when I apply a filter to the dataset. However, when I attempt to initate a sort on a GridView Column, I get :
Error: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.Common.DbDataRecord'
I think I've traced the root issue back to the bind method used in the sort:
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (eventArgs != null)
{
DataSet SortedData = new DataSet();
ds.Tables[0].DefaultView.Sort = eventArgs.SortExpression + " " + GetSortDirection(eventArgs.SortExpression);
SortedData.Tables.Add(ds.Tables[0].DefaultView.ToTable());
GridView1.DataSource = SortedData;
}
else
{
GridView1.DataSource = ds;
}
GridView1.DataBind();
}
I remember this was a major pain to figure out because only Dataset had a Sort Property
Since you assign a DataSet to GridView1.DataSource, then the type of e.Row.DataItem in the RowDatabound event will be a DataRowView instead of a System.Data.Common.DbDataRecord. You need to change this:
DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
to this:
DataRowView record = (DataRowView)e.Row.DataItem;
Instead of trying to sort the Dataset, I appended the sort direction and column from the viewstate to the query before binding it to the grid using SqlDataReader. Looks like I went out of my way to make a simple thing hard to begin with...

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.

Resources