我是第二次上这种论坛,第一次很感谢两位同胞回答了我的问题,但是我不会给分,我汗死,第一个问题是怎么给分?  第二个问题是一个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>
        &nbsp;</div>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </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

解决方案 »

  1.   

    首先
    DataKeyNames="clientID" 
    SqlDataAdapter   sda1   =   new   SqlDataAdapter("select   projectID,projectName   from   [clientProject]   where   clientID   =   '"   + GridView1.DataKeys[e.Row.RowIndex]["clientID"].ToString()  +   "'",   cn); 
      

  2.   

    <html   xmlns="http://www.w3.org/1999/xhtml"   > 
    <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();
                }
            } 
        }
    }
      

  3.   

    回复amandag:int型,嘿嘿谢谢你哈,我去试试
      

  4.   

    对了amandag同学,那个datakeyname是什么意思啊