使用来自数据库的数据加载网格中的垂直列

时间:2014-05-27 09:46:08

标签: c# asp.net

我想垂直加载网格列,数据来自数据库。请有人帮助我。我想在网格列中进一步编辑。下面是我的代码

  <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;
    }

1 个答案:

答案 0 :(得分:0)

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

如果您需要,请参阅此链接。它会帮助你

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