我是第一次使用模板列。我将第一列设为模板列,在item template里面放入一个TextBox控件(是放这里的吧?)Textbox空间名为Log01001(空间名是否就是GridView里的列名呢?)然后写了一下代码
protected void Page_Load(object sender, EventArgs e)
{
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = Session["Conn"].ToString();        string strCom = "select Log01001 from Log01";
        SqlDataAdapter ada = new SqlDataAdapter(strCom, conn);        DataSet Ds = new DataSet();
        ada.Fill(Ds, "tt");        this.C1WebGrid1.DataSource = Ds.Tables["tt"];
        this.C1WebGrid1.DataBind();
}数据并没有显示出来。请问,改怎么些那?模板列还真的没用过呢,谢谢!

解决方案 »

  1.   

    在Rowdatabound 里面写
    ((TextBox)e.Row.FindControl(控件名)).Text = 绑定数据
      

  2.   

    就是说Page_Load里面的操作都放到Rowdatabound 里面去么?是不是与模板列相关的数据操作都是在Rowdatabound 里面完成啊
      

  3.   

    Rowdatabound 这个是事件么?我grid喝text事件里面都找不到啊
      

  4.   

    还是没试出来我想在打开页面的时候就填充进去。protected void Page_Load(object sender, EventArgs e)
    {
    }protected void C1WebGrid1_DataBound(object sender, EventArgs e)
    {
    }
    能不能将稍微写些代码,让我看看是怎么做的呢?谢谢!
      

  5.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.FindControl("Label1") != null)
            {
                string lbl = (e.Row.FindControl("Label1") as Label).Text;
                lbl = DateTime.Parse(lbl).ToShortDateString();
                (e.Row.FindControl("Label1") as Label).Text = lbl;
            }
            if (e.Row.FindControl("Label2") != null)
            {
                string s = (e.Row.FindControl("Label2") as Label).Text;
                if (s != null && s != string.Empty)
                    s = DateTime.Parse(s).ToShortDateString();
                (e.Row.FindControl("Label2") as Label).Text = s;
            }
        }
      

  6.   

    谢谢。不过这个好像是将单一的数据传递进去吧?如果数据是从数据库里面出来的,我是不是能理解为
    我首先要将这些数据放在datagrid里面,但是要那列隐藏然后在RowDataBound事件里面写个循环,将隐藏列的值付给textbox呢?
    谢谢!
      

  7.   

    <asp:TemplateField HeaderText="City">
             <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
              </EditItemTemplate>
              <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("City") %>'></asp:Label>
               </ItemTemplate>
    </asp:TemplateField>既然你的TemplateField是Design-Time就决定了,why not直接设置Text='<%# Bind("City") %>'
    如果上面不是你所想要的简单方法,我到时候在给你一个RowDataBound处理的方法.
      

  8.   

    我想要那个RowDataBound的处理方法,^_^,谢谢
      

  9.   

    Assumption: "txtCity" is the ID of that textbox in your itemplate
                "City" is the filed name of data binding to that textbox
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim txtbox As TextBox
                txtbox = e.Row.FindControl("txtCity")
                If txtbox IsNot Nothing Then
                    Dim drv As Data.DataRowView
                    drv = CType(e.Row.DataItem, Data.DataRowView)
                    If drv IsNot Nothing Then
                        txtbox.Text = drv("city").ToString
                    End If
                End If
            End If
        End Sub
    End ClassLet me know if it works.
      

  10.   

    Let me know if it works.
      

  11.   

    protected void gvAssessAnswer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Visible = false;
    if (e.Row.FindControl("Label1") != null)
            {
                string lbl = (e.Row.FindControl("Label1") as Label).Text;
                lbl = DateTime.Parse(lbl).ToShortDateString();
                (e.Row.FindControl("Label1") as Label).Text = lbl;
            }        }
        }
      

  12.   

    像一些不需要经过处理直接调用的数据你应该直接在模板列里绑定
    ASPX页面
    <asp:TextBOX ID="Textbox1" runat="server Text='<%#Eval("tt")'%></TextBOx>
    这样在你DataBind()后数据直接就填充到这里了。
    如果你想对绑定的数据进行处理。。那么就需要在
    RowDataBound事件中进行处理,比如我想对上面的TEXT的值加几个字
    if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
    {
    TEXTBOX tt1=e.Row.findcontrol("textbox1")  as TEXTBOX;  //实例化对象
    tt1.text= "我的值是:" +tt1.text; //将值进行处理
        }
      

  13.   

    路过------------------------
    http://fenglin.xland.cn
    ------------------------