如何根据表中的行从gridview中的数据库和行中检索值?

时间:2013-10-22 11:20:01

标签: c# asp.net gridview datatable

我想根据数据库中的行

检索gridview中的值

我尝试过它的工作但控件中没有值

表格中有2行

修改

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="true">
    <Columns>
        <asp:TemplateField HeaderStyle-BackColor="DarkCyan" HeaderText="Activities">
            <ItemTemplate>
                <asp:DropDownList ID="dropActivities" Width="150px" runat="server"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Monday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Tuesday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Wednesday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Thursday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Friday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Saturday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Sunday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostback)
    {
      bind();
    }
}

private void bind()
{
    int rowIndex = 0;
    DataTable dt = new DataTable();
    DataRow drow = null;
    dt.Columns.Add(new DataColumn("Column0", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dt.Columns.Add(new DataColumn("Column4", typeof(string)));
    dt.Columns.Add(new DataColumn("Column5", typeof(string)));
    dt.Columns.Add(new DataColumn("Column6", typeof(string)));
    dt.Columns.Add(new DataColumn("Column7", typeof(string)));

        OleDbCommand cmd = new OleDbCommand("select activities,monday,tuesday,wednesday,thursday,friday,saturday,sunday from activity_values where week = '" + weekNumNow + "' and emp_id = (select emp_id from emp_master where username = '" + username + "')", DbConnection);
        DbConnection.Open();
        OleDbDataReader dr1 = cmd.ExecuteReader();

        while (dr1.Read())
        {
            drow = dt.NewRow();
            //Here I can get the values
            drow["Column0"] = dr1[0].ToString();
            drow["Column1"] = dr1[1].ToString();
            drow["Column2"] = dr1[2].ToString();
            drow["Column3"] = dr1[3].ToString();
            drow["Column4"] = dr1[4].ToString();
            drow["Column5"] = dr1[5].ToString();
            drow["Column6"] = dr1[6].ToString();
            drow["Column7"] = dr1[7].ToString();
            dt.Rows.Add(drow);
            dt.AcceptChanges();

            rowIndex++;
        }
        ViewState["CurrentTable"] = dt;
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

在gridview中添加行但显示空值

有什么想法吗?提前致谢

1 个答案:

答案 0 :(得分:0)

您需要使用Eval在gridview中显示数据,如下所示

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderStyle-BackColor="DarkCyan" HeaderText="Activities">
            <ItemTemplate>
                <asp:DropDownList ID="dropActivities" Width="150px" runat="server"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Monday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("monday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Tuesday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("tuesdady") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Wednesday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("wednesday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Thursday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("thursday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Friday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("friday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Saturday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("saturday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Sunday">
            <ItemTemplate>
                <asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("sunday") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

,您的bind()方法可以更简单,就像这样

private void bind()
{
    int rowIndex = 0;
    DataTable dt = new DataTable();

    OleDbCommand cmd = new OleDbCommand("select activities,monday,tuesday,wednesday,thursday,friday,saturday,sunday from activity_values where week = '" + weekNumNow + "' and emp_id = (select emp_id from emp_master where username = '" + username + "')", DbConnection);
    DbConnection.Open();
    OleDbDataReader dr1 = cmd.ExecuteReader();
    dt.Load(dr1);
    ViewState["CurrentTable"] = dt;
    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

dt.Load()会自动为dt创建所需的列。

修改如果要在gridview中使用<emptydatatemplate>显示数据输入的空行,请执行以下操作

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false">
    <Columns>
    .//
    .//keep same
    .//
    </Columns>
    <emptydatatemplate>
        <table>
            <tr>
                <td>
                    <asp:DropDownList ID="dropActivities" Width="150px" runat="server"></asp:DropDownList>
                </td>
                <td>
                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                </td>
                .//
                .//And so on
                .//
            </tr>
        </table>
    </emptydatatemplate>
</asp:GridView>