在网格中加载垂直列,数据's来自数据库

本文关键字:数据库 数据 网格 加载 垂直 | 更新日期: 2023-09-27 18:05:24

我想用来自数据库的数据垂直加载网格列。谁来帮帮我吧。我想在gridcolumn中进一步编辑。下面是我的代码

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
  <asp:GridView ID="GridView1" runat="server" Width="95%" style="text-align:center;"     GridLines="Both"
AutoGenerateColumns="false" DataKeyNames="WORKSHEET_ID"  HeaderStyle-ForeColor="White"      onrowediting="EditTaskStatus" onrowupdating="UpdateTaskStatus"  onrowcancelingedit="CancelTaskStatus"
AllowPaging ="true" emptydatatext="No Approval Task" OnPageIndexChanging = "OnPaging"   BackColor="AliceBlue" Font-Size = "11pt" AlternatingRowStyle-BackColor = "#F2F2F2"       RowStyle-BorderWidth="1" AlternatingRowStyle-BorderWidth="1"  
 HeaderStyle-BackColor = "#00829c" HeaderStyle-BorderColor="#CC9966" PagerStyle-CssClass="pagenation" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid"   EditRowStyle-Wrap="true"
 PageSize = "5">
 <Columns>
 <asp:TemplateField Visible="false" ItemStyle-Width = "30px"  HeaderText = "Id">
<ItemTemplate>
    <asp:Label ID="lblid" runat="server"
    Text='<%# Eval("WORKSHEET_ID")%>'></asp:Label>
</ItemTemplate> 
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "50px"  HeaderText ="Total Hours">
<ItemTemplate>
    <asp:Label ID="lblTotal" runat="server"
        Text='<%# Eval("TOTAL_HOURS")%>'></asp:Label>
</ItemTemplate>    
</asp:TemplateField>
 <asp:TemplateField HeaderText="Final Status" ItemStyle-Width = "50px">
              <ItemTemplate> 
                  <asp:Label ID="lblFinalStatus" runat="server" Text='<%#    Eval("MGR_STATUS") %>'></asp:Label>
              </ItemTemplate>
              <EditItemTemplate> 
                <asp:DropDownList runat="server"  ID="ddlTaskStatus">
                  <asp:ListItem Text="Accept" Value="1"></asp:ListItem>
                  <asp:ListItem Text="Decline" Value="2"></asp:ListItem>
                </asp:DropDownList>
                </EditItemTemplate>
             </asp:TemplateField>
   <asp:TemplateField  ItemStyle-Width = "100px"  HeaderText ="Comments">
   <EditItemTemplate>
     <asp:TextBox ID="txtComments" runat="server"
                 Text='<%# Eval("MGR_COMMENTS") %>'></asp:TextBox>
   </EditItemTemplate> 
  </asp:TemplateField>    
   <asp:TemplateField HeaderText="Action" ItemStyle-Width = "50px">
                    <ItemTemplate>
                       <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/images/edit.gif" Width="16" Height="16" />
                    </ItemTemplate>
                    <EditItemTemplate>
                       <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/images/action_check.gif" Width="16" Height="16" />
                       <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/action_delete.gif" Width="16" Height="16" />
                    </EditItemTemplate>
 </asp:TemplateField>
 </Columns>
  </asp:GridView>

请帮助我在后端我已经给出了代码

  protected void BindGridviewData()
    {
        DataTable dt = new DataTable();
        string strQuery = "SELECT * from Master_Worksheets where MGR_CODE ='" + lblUsername.Text + "' AND (MGR_STATUS='New' OR MGR_STATUS='Decline')";
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            SqlCommand cmd = new SqlCommand(strQuery);
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        gvconverted.DataSource = ConvertColumnsAsRows(dt);
        gvconverted.DataBind();
        gvconverted.HeaderRow.Visible = false;
    }
    public DataTable ConvertColumnsAsRows(DataTable dt)
    {
        DataTable dtnew = new DataTable();
        //Convert all the rows to columns
        for (int i = 0; i <= dt.Rows.Count; i++)
        {
            dtnew.Columns.Add(Convert.ToString(i));
        }
        DataRow dr;
        // Convert All the Columns to Rows
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            dr = dtnew.NewRow();
            dr[0] = dt.Columns[j].ToString();
            for (int k = 1; k <= dt.Rows.Count; k++)
                dr[k] = dt.Rows[k - 1][j];
            dtnew.Rows.Add(dr);
        }
        return dtnew;
    }

在网格中加载垂直列,数据's来自数据库

我猜你需要交换Datatable的行和列。

如果这是你需要的,那么请参考这个链接。它会帮助你的

http://www.codeproject.com/Articles/44274/Transpose-a-DataTable-using-C