【问题标题】:vb.net web application gridview to gridview dynamic sql where clausevb.net web应用gridview到gridview动态sql where子句
【发布时间】:2015-08-23 08:44:26
【问题描述】:

我几乎没有使用 vb.net 的经验。我的公司要求我开发一个 vb.net 网络应用程序。我在一个表单上有两个 GridView。在 Gridview1 上,我从 DB 菜单表中获取数据

select menu_name, menu_sys 
from menu 
where menu_sys <> '00' 
  and menu_grp = '00' 
  and menu_category = '00'

这会获取 GridView1 中的数据并显示出来。

我苦苦挣扎的部分是我想(鼠标)单击 GridView1 中的任何行选择该行,获取 menu_sys 值并将其用作 where 子句来检索 GridView2 的数据。

where 子句类似于

select menu_name, menu_grp 
from menu 
where menu_sys = 'MY-SELECTED-MENU_SYS-VALUE' 
  and menu_grp <> '00' 
  and menu_category = '00'

我不知道如何在运行时将参数添加到 vb.net 中的 sql 查询。我也不知道在哪里(哪个事件)在代码隐藏中编写上述代码逻辑。

任何帮助将不胜感激。

【问题讨论】:

  • 这是asp.net 还是winforms 应用程序?

标签: sql-server vb.net gridview


【解决方案1】:

您可以处理 SelectedIndexChanged 事件。这是一个例子:

标记:

<as:GridView runat="server" Id="GridView1" 
     OnSelectedIndexChanged="GridView1_SelectedIndexChaned"  ...

后面的代码:

protected void GridView1_SelectedIndexChaned(Object sender, GridViewSelectEventArgs e)
{
   // Get the currently selected row using the SelectedRow property.
   GridViewRow row = CustomersGridView.SelectedRow;
   var selectedId = row.Cells[1].Text; // assuming that the cell[1] has the id of the selected element

   //Now issue the second query and bind the data to the second grid:
   GridView2.DataSource = SomeMethodToGetTheDataUsingTheSelectedId(selectedId);
   GridView2.DataBind();
}

注意:不要将您的网格称为“GridView1”和“GridView2”,根据它们保存的数据类型使用适当的名称。

编辑

抱歉,我刚刚意识到您正在寻找一个 VB.NET 示例。看看MSDN:https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 2014-03-03
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多