如何从数据库中检索数据并将其放在gridview的文本框中

时间:2014-05-20 06:47:55

标签: c# asp.net

Problem on placing data in textbox in griview

问题是我最初使用gridview加载页面,每行有一行文本框。当我需要检索时,我需要gridview使得没有像DB一样存在的行,这样才能工作。但是gridview没有加载数据...... 我工作的代码......

int rowIndex = 0;
            ViewState["CurrentTable"] = dt.Tables[1];
            LoadGridViewfromDB();
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    System.Web.UI.WebControls.TextBox combobox = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[0].FindControl("combo");
                    System.Web.UI.WebControls.TextBox sizes = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[1].FindControl("sizes");
                    System.Web.UI.WebControls.TextBox barcodenumber = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[2].FindControl("barcodeno");
                    System.Web.UI.WebControls.TextBox ratio = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[3].FindControl("ratio");
                    System.Web.UI.WebControls.TextBox qty = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[4].FindControl("qty");
                    combobox.Text = dt.Tables[1].Rows[i - 1]["Combo1"].ToString();
                    sizes.Text = dt.Tables[1].Rows[i - 1]["Sizes"].ToString();
                    barcodenumber.Text = dt.Tables[1].Rows[i - 1]["Barcode_Number"].ToString();
                    ratio.Text = dt.Tables[1].Rows[i - 1]["Ratio"].ToString();
                    qty.Text = dt.Tables[1].Rows[i - 1]["Quantity"].ToString();
                    rowIndex++;
                }
                gvmonoprixmasters.DataSource = dtCurrentTable;
                gvmonoprixmasters.DataBind();
            }
        }

protected void LoadGridViewfromDB()
{
    try
    {
        DataTable dtCurrentTable =(DataTable)ViewState["CurrentTable"];
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = new DataTable();
            DataRow dr = null;

                dt.Columns.Add(new DataColumn("Col1", typeof(string)));
                dt.Columns.Add(new DataColumn("Col2", typeof(string)));
                dt.Columns.Add(new DataColumn("Col3", typeof(string)));
                dt.Columns.Add(new DataColumn("Col4", typeof(string)));
                dt.Columns.Add(new DataColumn("Col5", typeof(string)));
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                dr = dt.NewRow();
                dr["Col1"] = dtCurrentTable.Rows[i - 1]["Combo1"].ToString();
                dr["Col2"] = dtCurrentTable.Rows[i - 1]["Sizes"].ToString();
                dr["Col3"] = dtCurrentTable.Rows[i - 1]["Barcode_Number"].ToString();
                dr["Col4"] = dtCurrentTable.Rows[i - 1]["Ratio"].ToString();
                dr["Col5"] = dtCurrentTable.Rows[i - 1]["Quantity"].ToString();
                dt.Rows.Add(dr);
            }
            ViewState["CurrentTable"] = dt;
            gvmonoprixmasters.DataSource = dt;
            gvmonoprixmasters.DataBind();
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
    catch (Exception ee) { }
}

1 个答案:

答案 0 :(得分:1)

在Aspx页面

 <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="COl1">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Col2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Col3">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col3") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在aspx.cs页面中使用

 GridView1.DataSource = dt.Tables[1];
        GridView1.DataBind();