我是第二次上这种论坛,第一次很感谢两位同胞回答了我的问题,但是我不会给分,我汗死,第一个问题是怎么给分? 第二个问题是一个GRIDVIEW的嵌套。 ASPX部分的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="clientName" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="clientID" >
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>后台c#部分的代码是:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string ConnectionString = "Data Source=localhost;Initial Catalog=abs;User ID=sa;Password=123";
SqlConnection cn = new SqlConnection(ConnectionString);
cn.Open();
string str = "select clientName from [clientInfo]";
SqlDataAdapter sda = new SqlDataAdapter(str, cn);
DataSet dr = new DataSet();
sda.Fill(dr);
GridView1.DataSource = dr;
GridView1.DataBind();
cn.Close();
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ GridView oGridView = (GridView)e.Row.FindControl("GridView2");
if (oGridView != null)
{
string str1 = "select clientID from [clientInfo] where clientName = '" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString().Trim() + "'";
SqlDataAdapter sda1 = new SqlDataAdapter("select projectID,projectName from [clientProject] where clientID = '" + str1 + "'", cn);
DataSet dr1 = new DataSet();
sda1.Fill(dr1);
oGridView.DataSource = dr1;
oGridView.DataBind();
cn.Close(); }
} }
}最后运行出来的错误是Incorrect syntax near '中央银行'.
我有两个表
一个是clientInfo,结构如下:
clientName clientID(主建)
中央银行 1
花旗银行 2
还有一个表叫clientProject,结构如下
projectID(主健)projectName clientID
1 融资 1
2 收购 2
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="clientName" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="clientID" >
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>后台c#部分的代码是:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string ConnectionString = "Data Source=localhost;Initial Catalog=abs;User ID=sa;Password=123";
SqlConnection cn = new SqlConnection(ConnectionString);
cn.Open();
string str = "select clientName from [clientInfo]";
SqlDataAdapter sda = new SqlDataAdapter(str, cn);
DataSet dr = new DataSet();
sda.Fill(dr);
GridView1.DataSource = dr;
GridView1.DataBind();
cn.Close();
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ GridView oGridView = (GridView)e.Row.FindControl("GridView2");
if (oGridView != null)
{
string str1 = "select clientID from [clientInfo] where clientName = '" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString().Trim() + "'";
SqlDataAdapter sda1 = new SqlDataAdapter("select projectID,projectName from [clientProject] where clientID = '" + str1 + "'", cn);
DataSet dr1 = new DataSet();
sda1.Fill(dr1);
oGridView.DataSource = dr1;
oGridView.DataBind();
cn.Close(); }
} }
}最后运行出来的错误是Incorrect syntax near '中央银行'.
我有两个表
一个是clientInfo,结构如下:
clientName clientID(主建)
中央银行 1
花旗银行 2
还有一个表叫clientProject,结构如下
projectID(主健)projectName clientID
1 融资 1
2 收购 2
解决方案 »
- WPF取资源文件夹图片相对地址
- C# 键盘事件(如果不让任务管理器跳出来)
- inputPanel1.CurrentInputMethod = inputPanel1.InputMethods[0];
- 请问怎么在c#中获得当前已打开网站的标题,就是IE的标题栏文本
- contextMenuStrip的位置问题。
- 为什么VS2005中我打了"{",但是"}"不会自动填上。
- 页面刷新与提交的问题
- 请问c# mapx开发的如何添加图元
- C#的GetHashCode与JAVA的不对应,如何解决
- 有关PropertyGrid 弹出一个Form 紧急求助
- 部门汇总统计
- C# winform 使用parallel port 同步读取问题
DataKeyNames="clientID"
SqlDataAdapter sda1 = new SqlDataAdapter("select projectID,projectName from [clientProject] where clientID = '" + GridView1.DataKeys[e.Row.RowIndex]["clientID"].ToString() + "'", cn);
<head id="Head1" runat="server">
<title> 无标题页 </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="clientID" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="projectID">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string ConnectionString = "Data Source=localhost;Initial Catalog=abs;User ID=sa;Password=123";
SqlConnection cn = new SqlConnection(ConnectionString);
string str = "select clientID, clientName from [clientInfo]";
SqlDataAdapter sda = new SqlDataAdapter(str, cn);
DataSet dr = new DataSet();
cn.Open();
sda.Fill(dr);
cn.Close();
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView oGridView = (GridView)e.Row.FindControl("GridView2");
if (oGridView != null)
{
string ConnectionString = "Data Source=localhost;Initial Catalog=abs;User ID=sa;Password=123";
SqlConnection cn = new SqlConnection(ConnectionString);
SqlDataAdapter sda1 = new SqlDataAdapter("select projectID, projectName from [clientProject] where clientID = @clientID", cn);
sda1.SelectCommand.Parameters.Add("@clientID", SqlDbType.Int).Value = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
DataSet dr1 = new DataSet();
cn.Open();
sda1.Fill(dr1);
cn.Close();
oGridView.DataSource = dr1;
oGridView.DataBind();
}
}
}
}