模板列放在哪里都可以,没有什么只能放前面不能放后面的问题;修改时可以根据ItemIndex确定哪一行。

解决方案 »

  1.   

    1.请问是不是要用到模板列?模板列只能放在DataGrid前面两列使用,不能放在后面呀。
    ---当然可以放在后面,你调整布局就可以了
     2.如果加上按钮以后,比如点击修改的Button,怎么能确定是这一行的Button呢?
    ---在ItemCommand事件里面可以根据e.Item.Cell来访问其他绑定的数据,当然包括ID
      

  2.   

    1.请问是不是要用到模板列?模板列只能放在DataGrid前面两列使用,不能放在后面呀。
    ---模板列放在哪里都可以;对于你的问题用2个按钮列就可以 2.如果加上按钮以后,比如点击修改的Button,怎么能确定是这一行的Button呢?
    ---<asp:ButtonColumn Text="修改" CommandName="Modify"></asp:ButtonColumn>
    <asp:ButtonColumn Text="删除2" CommandName="DeleteImage"></asp:ButtonColumn>
    private void GridItem_ItemCommand(object source, DataGridCommandEventArgs e)
    {
            if(e.CommandName == "Modify")
                   ……………………        if(e.CommandName == "DeleteImage")
                   ………………}
      

  3.   

    private void GridItem_ItemCommand(object source, DataGridCommandEventArgs e)
    {
            if(e.CommandName == "Modify")
            {
                string tmp = e.Item.Cells[0].Text;//取该行第一列的值
                …………
            }        if(e.CommandName == "DeleteImage")
                   ………………}
      

  4.   

    html:
    <DataGrid ...... onItemCommand="GridItem_ItemCommand" ..../><columns>
    <asp:templatecolumn>
    <itemtemplate>
      <asp:linkbutton  commandname="change"....../>修改</asp:linkbutton>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemtemplate>
      <asp:linkbutton  commandname="delete"....../>删除</asp:linkbutton>
    </itemtemplate>
    </asp:templatecolumn>cs:
    private void GridItem_ItemCommand(object source, DataGridCommandEventArgs e)
    {
            if(e.CommandName == "Modify")
                   ……………………        if(e.CommandName == "DeleteImage")
                   ………………}
      

  5.   

    private void GridUserOrigin_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (e.CommandName=="Edit")//修改的摸板列

    //执行你的修改代码
    }
    if (e.CommandName=="Delete")//删除摸板列

    //执行你的删除代码
    }
    }
      

  6.   

    to: brightheroes
    怎么调整布局?
    grid是动态填充的。
    好像不能将模板列放在最后吧?
      

  7.   

    DataGrid的模板列
    基于模板的列在datagrid控件中扮演了一个很重要的角色,它们允许你增加任意一种类型的列到datagrid,datagrid通过纯文本的形式或者某种预定义的列类型显示内容。然而,有时预定义的列类型并不能实现我们想要的东西。模板列有四种不同的模板,如图1
    Figure 1 DataGrid Column Templates 
    Name Description
    ItemTemplate Contains the template for the items in a DataGrid's column. 
    <ItemTemplate>
      <asp:label runat="server" text= '<%# ... %>'...>
    </ItemTemplate>
    You can use any combination of HTML text and ASP.NET controls to populate the column.
    EditItemTemplate Controls the contents of the item selected for editing in the column of the DataGrid control. Place the controls you need for editing the cell between the opening and closing EditItemTemplate tags. 
    <EditItemTemplate>
      <asp:textbox runat="server" text= '<%# ... %>'...>
    </EditItemTemplate>
    HeaderTemplate Contains the template for the heading section. 
    <HeaderTemplate>
      <asp:label runat="server" text= "Header"...>
    </HeaderTemplate>
    If you omit this template, the column header is rendered with a label or a hyperlink if sorting is enabled. By specifying a custom template, you make yourself responsible to provide the user interface needed to enable sorting on the column.
    FooterTemplate Contains the template for the footer section of the column. The default value is a null reference. 
    <FooterTemplate>
      <asp:label runat="server" text= "..."...>
    </FooterTemplate>
    The footer is displayed only if the ShowFooter property of the DataGrid is set to True.你可能会频繁的使用模板列(ItemTemplate)。它定义了怎么样显示列中的单元格及由哪些元素组成控件的内容。HeaderTemplate 和 FooterTemplate我们就不说了,相信大家都知道是干什么用的。当列的所属行进入编辑模式时,EditItemTemplate属性指明单元格应该怎样变化。但要注意的是,它和datalist控件不一样,datagrid没有选中模板。
    当你需要用不规范的方式显示整个列的时候,你的DataGrid应该用基于模板的列。如果你需要显示数据的数据无法用datagrid 提供的普通的模板显示时,这时,模板就是你的最佳选择了。
      

  8.   

    谢谢各位,结帐。只是不能动态将模板列加来DataGrid的右边,
    是个小小的遗憾!