DataGrid显示双层表头
假设你的DataGrid有三列,现在想将前两列作为"大类1",第三列作为"大类2",现在,你可以在ItemDataBound事件中加入下面的代码:
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.Cells[0].ColumnSpan = 2;
e.Item.Cells[0].Text = "大类1</td><td>大类2</td></tr><tr><td>" + e.Item.Cells[0].Text;
}
用这个方法可以为任意添加新行。

解决方案 »

  1.   

    为了为浏览者提供一个空行,我们使用DataGrid的Footer Template,我们直接在Footer Template里添加文本框,这样可以避免不必要的操作:比如点击“编辑”按钮等。这样也可以减少往复数据提交的次数。我们这里仍然LinkButton(插入),并设置CommandName属性为“Insert”,这个CommandName在DataGrid的ItemCommand事件中,确保只有用户点击了“Insert”LinkButton才添加记录。添加到数据库的方法是很简单的。 
    在ItemDataBound里面:
        If e.CommandName = "Insert" Then
          Dim cnn As New SqlConnection(connstr)
          Dim t1 As TextBox = e.Item.FindControl("textbox2")
          Dim t2 As TextBox = e.Item.FindControl("textbox4")
          cnn.Open()
          Dim cmd As New SqlCommand("insert into employees(lastname,firstname) values('" & t1.Text & "','" & t2.Text & "')", cnn)
          cmd.ExecuteNonQuery()
          cnn.Close()
          BindGrid()
        End If
      

  2.   

    http://www.dotnetjunkies.com/Tutorial/ShowContent.aspx?cg=C0DD0D9A-8907-4117-BBF2-4C407AA83574&ForumID=4064
      

  3.   

    To :acewang(**^o^**) 
    谢谢,
    可是我的FooterTemplate为什么不显示呢
    这个DataGrid1该怎么写呢
      

  4.   

    seesea125(雨天) ,请用datagrid的itemdatacreat事件来动态加载你的文本框,这是元素生成阶段的事件
    而你说的的itemdatabound是绑定数据时的时间。