简体   繁体   中英

C# Question about Data Grid

I have the buttons populated in a DataGrid, now I want them to show on the right of the data not the default position (left). How can I achieve this?

Thank you

<asp:DataGrid runat="server" ID="myDataGrid"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" 
GridLines="None">
<Columns>
   <asp:TemplateColumn>
       <ItemTemplate>
          <asp:Button runat="server" Text="Delete"/>
        </ItemTemplate>
   </asp:TemplateColumn>
</Columns>

PS: I am binding the data to an array

  myDataGrid.DataSource = vals;
  myDataGrid.DataBind();

You can define a column for the array of string before the button column like this

<Columns>
   <asp:TemplateColumn>
       <ItemTemplate>
          <asp:Label ID="someLabelId" runat="server" 
                                              Text='<%# Container.DataItem %>' />
        </ItemTemplate>
   </asp:TemplateColumn>

   <asp:TemplateColumn>
       <ItemTemplate>
          <asp:Button runat="server" Text="Delete"/>
        </ItemTemplate>
   </asp:TemplateColumn>
</Columns>

and set AutoGenerateColumns="false" for the DataGrid

<asp:DataGrid runat="server" ID="myDataGrid" AutoGenerateColumns="false"

use ItemStyle HorizontalAlign="Right" as defined at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemstyle.aspx

try:

&ltasp:TemplateColumn ItemStyle-HorizontalAlign="Right">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM