从数据库C#中检索数据后,使用文本框将信息添加到gridview中

时间:2017-09-18 03:43:54

标签: c# asp.net visual-studio-2012 datagridview textbox

我从数据库中检索数据到5列的网格中,还有另外3列,我想用文本框输入填充(使用不同的按钮)。

从数据库中检索数据后,如何从文本框中将输入提取到网格中?

这是正面看起来像:

enter image description here

以下是编码:

<form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" Text="BNE No: "></asp:Label>
    &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label2" runat="server" Text="BNE No: "></asp:Label>
    &nbsp;<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search From Database" />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    <br />
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> 
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            <asp:Button ID="Button3" runat="server" Text="Insert to Grid" OnClick="Button3_Click" />
    <br />
    <%--<asp:Table ID="Table1" runat="server">
    </asp:Table>--%>

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

            <Columns>
            <asp:TemplateField HeaderText="BNE No">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%#Bind("BNENo") %>'></asp:Label>  
                    </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="ECN No">
                <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%#Bind("KVNo") %>'></asp:Label>  
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Rec Date">
                <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%#Bind("ReceivedDate") %>'></asp:Label>  
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Issued Date">
                <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%#Bind("IssuedDate") %>'></asp:Label>  
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%#Bind("Status") %>'></asp:Label>  
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Location">
                <ItemTemplate>
                    <asp:Label ID="Label9" runat="server" Text=''></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Time">
                    <ItemTemplate>
                    <asp:Label ID="Label10" runat="server" Text=''></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date">
                    <ItemTemplate>
                    <asp:Label ID="Label11" runat="server" Text=''></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>
            </Columns>
     </asp:GridView>
</div>
</form>

这是我用来通过按钮1

检索数据的后面代码
protected void Button1_Click(object sender, EventArgs e)//this Button use To call multiple data from database named tblBNE into GridView
{
    count = 0;
    con.Open();

    OleDbCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT [BNENo],[KVNo],[ReceivedDate],[IssuedDate],[Status] FROM [tblBNE] where BNENo='" + TextBox1.Text + "' or BNENo='" + TextBox2.Text + "' ";

    cmd.ExecuteNonQuery();

    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    da.Fill(dt);

    count = Convert.ToInt32(dt.Rows.Count.ToString());

    GridView1.DataSource = dt;
    GridView1.DataBind();

    con.Close();

    if (count == 0)
    {
        Label3.Text = "record not found";
    }
}

所以,我想使用按钮3 =插入网格以使用textbox3到5填充label9到11.我该怎么做?

提前谢谢你:)

0 个答案:

没有答案