如何对以下代码进行gridview排序? [英] how to do gridview sorting for the folllowing code?
本文介绍了如何对以下代码进行gridview排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够根据2个下拉列表选择对gridview进行排序。但是如果假设下拉列表选择的文本没有任何相应的记录,那么它必须显示错误消息。如何检查这种情况。下面的代码在选择下拉列表项并按下搜索按钮时对gridview进行排序。请注意:我的下拉列表在gridview之外。
im able to sort gridview based on 2 dropdown list selection. But if suppose dropdown list selected text doesnt have any corresponding records then it must display error msg . how to check this condition. below code sorts gridview when dropdown list items selected and search button pressed. Please note: My dropdown lists are outside gridview.
protected void btnSearch_Click(object sender, EventArgs e)
{
string constr = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
if (ddlMainmenu.SelectedIndex != 0 && ddlStentCat.SelectedIndex != 0)
{
cmd = new SqlCommand("SearchStentRecords_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "SELECT1");
cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
cmd.Parameters.AddWithValue("@ddl2", ddlStentCat.SelectedItem.Text);
adp.SelectCommand = cmd;
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
//da = new SqlDataAdapter("select Addstock.*, Tbl_StentUsageStock.Patient_name,Tbl_StentUsageStock.IP_no,Tbl_StentUsageStock.Date,Tbl_StentUsageStock.Technician_name FROM Addstock INNER JOIN Tbl_StentUsageStock ON Addstock.StentId=Tbl_StentUsageStock.StentId where Addstock.Device='" + ddlMainmenu.SelectedValue + "' AND Addstock.Category='" + ddlStentCat.SelectedValue + "' AND Addstock.Used='Y' ", con);
}
else if (ddlMainmenu.SelectedIndex != 0 && ddlStentCat.SelectedIndex == 0)
{
//da = new SqlDataAdapter("select Addstock.*, Tbl_StentUsageStock.Patient_name,Tbl_StentUsageStock.IP_no,Tbl_StentUsageStock.Date,Tbl_StentUsageStock.Technician_name FROM Addstock INNER JOIN Tbl_StentUsageStock ON Addstock.StentId=Tbl_StentUsageStock.StentId where Addstock.Device='" + ddlMainmenu.SelectedValue + "' AND Addstock.Used='Y' ", con);
cmd = new SqlCommand("SearchStentRecords_Sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "SELECT2");
cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
adp.SelectCommand = cmd;
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
}
}
}
推荐答案
public static void SortGridView(GridView gridViewId, string pSortExp, DataTable dtTableToSort, string pSortDirection)
{
VS_CommonMethods.ReUsableTryCatch(() =>
{
HttpContext.Current.Session["sortDirection"] = (VS_Utils.ToString(HttpContext.Current.Session["sortDirection"]) == VS_Constants.ASC) ? VS_Constants.DESC : VS_Constants.ASC;
DataView dvSearch = new DataView(dtTableToSort);
//Sorting the data view with given expression and direction
dvSearch.Sort = pSortExp + " " + pSortDirection;
//Set Grid pageIndex to 0
gridViewId.PageIndex = 0;
//Bind Data to gridView
FillGridDatasource(gridViewId, dvSearch.ToTable());
});
}
这篇关于如何对以下代码进行gridview排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文