C# 从一个表中选择dropdownlist值,并从另一个表ASP.NET中显示gridview数据
我创建了一个web应用程序,其中有一个下拉列表和一个Gridview。我从DB填充下拉列表值,我需要在网格视图中显示所选值的数据,但数据来自另一个表。我不知道怎么做,请任何人帮我。我的代码如下。提前谢谢 代码隐藏C# 从一个表中选择dropdownlist值,并从另一个表ASP.NET中显示gridview数据,c#,asp.net,gridview,drop-down-menu,C#,Asp.net,Gridview,Drop Down Menu,我创建了一个web应用程序,其中有一个下拉列表和一个Gridview。我从DB填充下拉列表值,我需要在网格视图中显示所选值的数据,但数据来自另一个表。我不知道怎么做,请任何人帮我。我的代码如下。提前谢谢 代码隐藏 protected void Page_Load(object sender, EventArgs e) { try { if (!IsPostBack) {
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
dropDownData.DataSource = dt;
dropDownData.DataTextField = "Work_Packages";
dropDownData.DataValueField = "Work_Packages";
dropDownData.DataBind();
Bind_Gridview();
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('" + ex.Message + "');</script>", false);
}
}
public void Displaysearchresult(string strSearch)
{
SqlCommand cmd = new SqlCommand("Select * from ArR_Station_Work_Packages Where Station_Name like'" + txtstationName.Text + "%'", myConnection);
cmd.Parameters.AddWithValue("@Work_Packages", strSearch);
cmd.Connection.Open();
dropDownData.DataSource = cmd.ExecuteReader();
dropDownData.DataTextField = "Work_Packages";
dropDownData.DataValueField = "Work_Packages";
dropDownData.DataBind();
}
protected void GetWorkPackages(object sender, EventArgs e)
{
if (txtstationName.Text == "" | txtstationName.Text == null)
{
}
else
{
Displaysearchresult(txtstationName.Text);
}
}
protected void ddlSelectedData(object sender, EventArgs e)
{
Bind_Gridview();
}
private void Bind_Gridview()
{
SqlCommand cmd = new SqlCommand("Select * from ArR_WP_BOQ_Details Where BOQ_ID like'" + dropDownData.SelectedValue + "'", myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dt.Clear();
da.Fill(dt);
myConnection.Close();
ViewState["BOQ"] = null;
ViewState["BOQ"] = (DataTable)dt;
addnewrow();
grd_BOQ.DataSource = dt;
grd_BOQ.DataBind();
}
private void addnewrow()
{
if (ViewState["BOQ"] != null)
{
DataTable idt = (DataTable)ViewState["BOQ"];
DataRow drCurrentRow = null;
DataRow drCurrentRow_lastrow = null;
int New_gridrowcount = idt.Rows.Count;
drCurrentRow = idt.NewRow();
drCurrentRow_lastrow = idt.NewRow();
if (idt.Rows.Count >= 10)
{
idt.Rows.InsertAt(drCurrentRow, 9);
idt.Rows.InsertAt(drCurrentRow_lastrow, idt.Rows.Count);
}
else
{
idt.Rows.Add(drCurrentRow);
}
ViewState["BOQ"] = idt;
}
}
protected void grd_BOQ_PageIndexChanged(object sender, GridViewPageEventArgs e)
{
Bind_Gridview();
grd_BOQ.PageIndex = e.NewPageIndex;
grd_BOQ.EditIndex = -1;
grd_BOQ.DataBind();
}
protected void grd_BOQ_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
int grd_lastpgcount = grd_BOQ.PageIndex;
dt = (DataTable)ViewState["BOQ"];
int count = dt.Rows.Count;
if (e.Row.RowType == DataControlRowType.DataRow || !string.IsNullOrEmpty(dropDownData.SelectedValue))
{
int rowcreate_rowindex = e.Row.RowIndex;
if (rowcreate_rowindex < count - 1 && rowcreate_rowindex != 9 && grd_lastpgcount * 10 + rowcreate_rowindex + 1 < count)
{
for (int rc = 0; rc < 1; rc++)
{
edit_button = new ImageButton();
edit_button.ID = "edit_button";
edit_button.ImageUrl = "~/images/edit.png";
edit_button.Style.Add("width", "20px");
edit_button.Style.Add("height", "20px");
edit_button.CommandName = "Edit";
edit_button.ToolTip = "Edit";
e.Row.Cells[rc].Controls.Add(edit_button);
update_button = new ImageButton();
update_button.ID = "update_button";
update_button.ImageUrl = "~/images/save.png";
update_button.Style.Add("width", "20px");
update_button.Style.Add("height", "20px");
update_button.CommandName = "Update";
update_button.ToolTip = "Update";
e.Row.Cells[rc].Controls.Add(update_button);
if (onrow_edit_flag == rowcreate_rowindex)
{
edit_button.Visible = false;
update_button.Visible = true;
}
else
{
edit_button.Visible = true;
update_button.Visible = false;
}
}
}
else if (rowcreate_rowindex == count - 1 || rowcreate_rowindex == 9 || grd_lastpgcount * 10 + rowcreate_rowindex + 1 == count)
{
for (int rc = 0; rc < 1; rc++)
{
add_button = new ImageButton();
add_button.ID = "add_button";
add_button.ImageUrl = "~/images/Add.png";
add_button.Style.Add("width", "20px");
add_button.Style.Add("height", "20px");
add_button.Click += new ImageClickEventHandler(insert_button_Click);
add_button.ToolTip = "Add";
e.Row.Cells[rc].Controls.Add(add_button);
update_button = new ImageButton();
update_button.ID = "update_button";
update_button.ImageUrl = "~/images/save.png";
update_button.Style.Add("width", "20px");
update_button.Style.Add("height", "20px");
update_button.Click += new ImageClickEventHandler(insert_save_click);
update_button.ToolTip = "Add";
e.Row.Cells[rc].Controls.Add(update_button);
if (onrow_create_flag == rowcreate_rowindex || dt.Rows.Count % grd_BOQ.PageSize == onrow_create_flag)
{
add_button.Visible = false;
update_button.Visible = true;
}
else
{
add_button.Visible = true;
update_button.Visible = false;
}
}
}
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('" + msg + "');</script>", false);
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
尝试
{
如果(!IsPostBack)
{
dropDownData.DataSource=dt;
dropDownData.DataTextField=“工作包”;
dropDownData.DataValueField=“工作包”;
dropDownData.DataBind();
Bind_Gridview();
}
}
捕获(例外情况除外)
{
字符串msg=ex.Message.ToString();
ScriptManager.RegisterStartupScript(此,typeof(页),“警报”,“警报(“+”+ex.Message+“);”,false);
}
}
public void Displaysearchresult(字符串strSearch)
{
SqlCommand cmd=new-SqlCommand(“从ArR_Station_Work_包中选择*,其中Station_名称类似“'+txtstationName.Text+“%”,myConnection);
cmd.Parameters.AddWithValue(“@Work\u Packages”,strSearch);
cmd.Connection.Open();
dropDownData.DataSource=cmd.ExecuteReader();
dropDownData.DataTextField=“工作包”;
dropDownData.DataValueField=“工作包”;
dropDownData.DataBind();
}
受保护的void GetWorkPackages(对象发送方、事件参数e)
{
if(txtstationName.Text==“”| txtstationName.Text==null)
{
}
其他的
{
显示搜索结果(txtstationName.Text);
}
}
受保护的无效ddlSelectedData(对象发送方、事件参数e)
{
Bind_Gridview();
}
私有void Bind_Gridview()
{
SqlCommand cmd=new SqlCommand(“从ArR_WP_BOQ_详细信息中选择*,其中BOQ_ID类似“”+dropdownddata.SelectedValue+”,myConnection);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
dt.Clear();
da.填充(dt);
myConnection.Close();
ViewState[“BOQ”]=null;
视图状态[“BOQ”]=(数据表)dt;
addnewrow();
grd_.DataSource=dt;
grd_工程量清单数据绑定();
}
私有void addnewrow()
{
如果(ViewState[“BOQ”]!=null)
{
DataTable idt=(DataTable)视图状态[“BOQ”];
DataRow drCurrentRow=null;
DataRow drCurrentRow_lastrow=null;
int New_gridrowcount=idt.Rows.Count;
drCurrentRow=idt.NewRow();
drCurrentRow_lastrow=idt.NewRow();
如果(idt.Rows.Count>=10)
{
idt.Rows.InsertAt(drCurrentRow,9);
idt.Rows.InsertAt(drCurrentRow\u lastrow,idt.Rows.Count);
}
其他的
{
idt.Rows.Add(drCurrentRow);
}
ViewState[“BOQ”]=idt;
}
}
受保护的无效grd\U BOQ\U页面索引交换(对象发送方,GridViewPageEventArgs e)
{
Bind_Gridview();
grd_BOQ.PageIndex=e.NewPageIndex;
grd_BOQ.EditIndex=-1;
grd_工程量清单数据绑定();
}
受保护的无效grd\U BOQ\U RowCreated(对象发送方,GridViewRowEventArgs e)
{
尝试
{
int grd_lastpgcount=grd_BOQ.PageIndex;
dt=(数据表)视图状态[“BOQ”];
int count=dt.Rows.count;
如果(e.Row.RowType==DataControlRowType.DataRow | |!string.IsNullOrEmpty(dropDownData.SelectedValue))
{
int rowcreate_rowindex=e.Row.rowindex;
如果(rowcreate_rowindex