通过选择第一个网格行显示其他网格行的值 [英] Show value of an other grid row by the selection of the first grid row
本文介绍了通过选择第一个网格行显示其他网格行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网格视图,我只使用一列,现在问题是当我试图选择进入第一个网格视图时,相应的结果没有显示在第二个也是网格视图plz告诉我
这是代码:
I have a grid view for which i am using only one column and now what is the problem when i am trying to select into first grid view the respectively result is not showing in the second one which is also grid view plz tell me
Here's the code:
[WebMethod]
public List<properties> GetStates()
{
SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from States", oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties = new List<properties>();
using (SqlDataReader OReader = oSqlCommand.ExecuteReader())
{
while (OReader.Read())
{
properties Oproperties = new properties();
Oproperties.Id = Convert.ToInt32(OReader["State_ID"].ToString());
Oproperties.State = OReader["State"].ToString();
oListproperties.Add(Oproperties);
}
}
return oListproperties;
}
[WebMethod]
public List<properties> GetCity(int ID)
{
//properties Oproperties1 = new properties();
//Oproperties1.Id = StateID;
SqlConnection oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand oSqlCommand = new SqlCommand("select * from City where State_ID ="+ID , oSqlConnection);
oSqlCommand.CommandType = CommandType.Text;
oSqlConnection.Open();
List<properties> oListproperties=new List<properties>();
using(SqlDataReader OReader = oSqlCommand.ExecuteReader())
{while(OReader.Read())
{properties Oproperties=new properties();
Oproperties.Id=Convert.ToInt32(OReader["ID"].ToString());
Oproperties.City=OReader["City"].ToString();
oListproperties.Add(Oproperties);
}
return oListproperties;
}
}
推荐答案
如下:
as follows :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
{
SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT CategoryID, CategoryName,Description FROM Categories", connection);
DataSet dataSet = new DataSet();
dataAdpater.Fill(dataSet, "Categories");
if (dataSet.Tables.Contains("Categories"))
{
gvMain.DataSource = dataSet.Tables["Categories"].DefaultView;
gvMain.DataKeyNames = new string[] { "CategoryID" };
gvMain.DataBind();
}
}
}
}
protected void gvMain_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
{
SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT * FROM Products where CategoryID=@CategoryID ", connection);
dataAdpater.SelectCommand.Parameters.Add(new SqlParameter("@CategoryID", gvMain.DataKeys[e.NewSelectedIndex].Value));
DataSet dataSet = new DataSet();
dataAdpater.Fill(dataSet, "Products");
if (dataSet.Tables.Contains("Products"))
{
gv.DataSource = dataSet.Tables["Products"].DefaultView;
gv.DataBind();
}
}
}
这篇关于通过选择第一个网格行显示其他网格行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文