将数据添加到网格视图,然后将其检索并添加到数据库 [英] Add data to grid view and then retrieve it and add to a databse

查看:93
本文介绍了将数据添加到网格视图,然后将其检索并添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,
首先我的英语不好..pls不要介意.我需要asp .net专家的帮助.因为我不是asp .net的专家.我正在做一个学术项目.为此,需要某种形式的帮助.以帐单的形式..我尝试了很多遍,并在整个网络中进行了搜索..我在这些方面没有足够的知识.这就是为什么要问你们盖伊..我希望你们都帮助我..它非常紧急的朋友... :( ..pls帮助

首先,我向您展示我的两个表连接到此操作
商品"数据库图片
销售"数据库图片

接下来,我将向您展示我的表单的演示版面

表单布局

在表格中您可以看到一个dropdowlist.in该列表中我绑定了"item"数据库中的"item_code" ...当加载表格时,所有项目都将加载到该dropdownlist中..next是一个文本框...要输入我们的数量我们当时要输入的商品的数量...当时要输入的...然后,您可以看到一个名为"ADD TO DATA GRID"的按钮..单击该按钮时..要在gridview中添加一行包含item_code的项目我们之前在下拉列表和要在文本框中输入的数量中进行了选择..并且想在网格视图中再添加3个Cloum大小",项目名称",税".这些数据想从项目"数据库中获取,例如"select大小,项目名称,税项,例如item_code的项目,例如"+ dropdownlist.selcteditem +";" ...我认为您已经明白了我所说的内容...并且我认为u Guyz知道了该删除按钮..单击时要删除该特定行...

接下来你可以看到一个标签..total ..有想要添加价格*数量,我们添加到网格..意味着如果我添加了一个项目,并且价格为50,数量为2 ..总和为100,那么我添加了另一个项目的价格为20,数量为2,然后将总数增加40(20 * 2),则总数将为140(旧的100 + 40).
最后,您可以看到一个名为最终提交"的按钮..按此按钮时...想要将一行一行添加到gridview的"sales"数据库中,而某些数据想从"items"数据库中获取.意味着在gridview中仅具有item_code,名称,价格,大小和税项..当单击最终提交"按钮时,要将db的整个9个字段逐个添加到sales db中,而在gridview中...这就是全部


我知道这是一个大问题.但是我认为U GUYZ是ASP .NET的专家,U GUYZ可以轻松完成...所以..PLS使它像这种形式(C#更好)..请与我分享此处的aspx和aspx.cs文件代码...
请帮助Guyz ..
提前感谢朋友..我希望你能帮助我..如果有任何疑问您可以在这里提出.

Hi friends,
first of all my english is not good..pls dont mind.i need help from asp .net experts.because i am not expert in asp .net.i am doing a acadamic project.for that need a help in a form..in billing form..i tried many times and searched in whole net..i have no good knowlege in these..thats why asking to you guyz..i hope u all help me..its very urgent friends... :(..pls help

first of all i show u my two tables connect to this operation
"item" DB pic
"sales" DB pic

and next i will show you my demo layout of my form

form layout

in the form u can see a dropdowlist.in that list i binded "item_code" from "item" DB...when load form all items will load in that dropdownlist..next is a textbox...it is to enter our quantity of item we purchased...that we want to enter at that time...then u can see a button called "ADD TO DATA GRID"..when click on it.. want to add a row to gridview that contain item_code which we selected before on dropdowlist and quantity which we enter in textbox..and want to add 3 more cloums in to gridview "size","item_name","tax"..these datas want to fetch from "items" DB like "select size,item_name,tax from items where item_code like ''"+dropdownlist.selcteditem+"'';"...i think u got it what i said...and i think u guyz know about that delete button..when click on it..want to delete that specific row...

next u can see a label..total..there want to add the price*quanity which we adding to grid..means if i added a item and that have price 50 and quanity 2..the total will be 100 then i added another item have price 20 and quantity 2 then add 40(20*2) to total then total will be 140(old 100 + 40)..like that...

and finally u can see a button called "final submit"..when pressing this button...want to add the the one by one row to "sales " DB from gridview and some data want to fetch from "items" DB..means in gridview only have item_code,name,price,size and tax..when click on "final submit" button want to add whole 9 fields of items db to sales db one by one have in gridview...thats all


I KNOW FRIENDS IT IS BIG QUESTION..BUT I THINK U GUYZ ARE EXPERT IN ASP .NET AND U GUYZ CAN DO EASLY...SO..PLS MAKE A FORM LIKE THIS(C# IS MORE GOOD)..AND PLS SHARE ME THAT aspx and aspx.cs file codes here...
pls help guyz..
advance thanks friends..i hope u guyz will help me..if have any doubts u can ask here.

推荐答案

hi , Hope  it help you


DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from  Items", con);
        cmd.CommandType = CommandType.Text;
        dt = new DataTable();
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "item_code";
        DropDownList1.DataValueField = "item_code";
        DropDownList1.DataBind();
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cmd.Dispose();
        con.Close();
        ViewState.Add("dt", dt);
    }

}

protected void Button1_Click(object sender, EventArgs e)
{
    dt = (DataTable)ViewState["dt"];
    int tax = 0;
    int price = 0;
    int Qty = 0;
    string name = "";
    int size = 0;
    foreach (GridViewRow item in GridView1.Rows)
    {
        if (item.Cells[1].Text ==DropDownList1.SelectedValue)
        {
              tax =Convert.ToInt32( item.Cells[6].Text);
              price =Convert.ToInt32( item.Cells[4].Text);
              Qty =Convert.ToInt32( item.Cells[5].Text);
              size = Convert.ToInt32(item.Cells[3].Text);
            name=item.Cells[2].Text;

        }
    }
    int QtyNew = 0;
    int Total = 0;
    int totalAll = 0;
    foreach (DataRow item in dt.Rows)
    {
        if (item[0].ToString() == DropDownList1.SelectedValue )
        {
            QtyNew = Convert.ToInt32(item[7]) + Convert.ToInt32(TextBox1.Text);
            item[7] = QtyNew.ToString();
                if (ViewState["total"] !=null)
                {
                    totalAll = Convert.ToInt32(ViewState["total"]);
                }
            Total = (price * QtyNew ) + totalAll;
            ViewState.Add("total", Total);
        }
    }
    lblTotal.Text = Total.ToString();
   GridView1.DataSource = null;
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    dt = (DataTable)ViewState["dt"];
    dt.Rows[e.RowIndex].Delete();
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=IT-DEV2\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from  Items", con);
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    dt = (DataTable)ViewState["dt"];
    adpt.Fill(ds);
    SqlCommandBuilder scb = new SqlCommandBuilder(adpt);
    adpt.UpdateCommand = scb.GetUpdateCommand();
    adpt.Update(dt);
    Response.Write("<script>alert('saved')");
}







<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="item code"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="Qty"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="229px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

            Text="ADD TO DATA GRID" />

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

        onrowdeleting="GridView1_RowDeleting">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="item_code" HeaderText="item_code" />
            <asp:BoundField DataField="Item_name" HeaderText="Item_name" />
            <asp:BoundField DataField="size" HeaderText="size" />
            <asp:BoundField DataField="price" HeaderText="price" />
            <asp:BoundField DataField="Qty" HeaderText="Qty" />
            <asp:BoundField DataField="tax" HeaderText="tax" />
        </Columns>
    </asp:GridView>
    <asp:Label ID="Label3" runat="server" Text="Total :"></asp:Label>  &nbsp;
    <asp:Label ID="lblTotal" runat="server"></asp:Label>   <br />
    <asp:Button ID="Button2" runat="server" Text="Final Submit"

        onclick="Button2_Click" />
    </form>
</body>
</html>


这篇关于将数据添加到网格视图,然后将其检索并添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆