如何更新DataTable? [英] How to Update DataTable ?

查看:52
本文介绍了如何更新DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了gridView with datasource是dataTable。 GridView在templatefield中包含textBox。我的问题是当我点击更新按钮时,我想用textBox的值更新Gridview ..



我的GridView

 <   asp: GridView     ID   =  GridView1    runat   =  server   宽度  =  100%    CssClass   =  scrtablegrid    GridLines   =     

onrowdatabound = GridView1_RowDataBound ShowFooter = True

AutoGenerateColumns = 错误 onrowupdating = GridView1_RowUpdating >
< AlternatingRowStyle CssClass = scrgridbg2 / < span class =code-keyword>>

< HeaderStyle CssClass = < span class =code-keyword> scrgridheader 高度 = 30px Horizo​​ntalAlign = 字体下划线 = true ForeColor = 白色 / >
< RowStyle CssClass = scrtablegrid 高度 = 40px Horizo​​ntalAlign = / >
< < span class =code-keyword>>
< asp:BoundField DataField = Rno HeaderText = / >
< asp:TemplateField HeaderText = 项目 > ;
< ItemTemplate >
< asp:标签 ID = Label2 runat = < span class =code-keyword> server 文本 =' <% #Bind( Item) %>' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 数量 >

< ItemTemplate >
[< a href = class = removeqty > 删除< / a > ] < br / >
< asp:TextBox ID = txtQty runat = < span class =code-keyword> server 文本 =' <% #Bind( 数量)%>' class = qtyinput / >

< / ItemTemplate >
< / asp:TemplateField >
< asp:BoundField < span class =code-attribute> HeaderText = 可用 / > ;
< asp:BoundField DataField = 价格 HeaderText = 价格 DataFormatString = {0:c} / > ;
< asp:BoundField HeaderText = 总数 DataField = 总计 / >
< /列 >
< FooterStyle CssClass = mitemstotalbg 高度 = 30px Horizo​​ntalAlign = < span class =code-attribute> / >
< / asp:GridView >





代码后面的页面

 public void btnGo_Click (对象发送者,EventArgs e)
{
if(Session [CurrentData]!= null)
{
DataTable dt =(DataTable)Session [CurrentData];
int count = dt.Rows.Count;
BindGrid(伯爵);
}
其他
{
BindGrid(1);
}

}





 private void BindGrid(int rowcount)
{

string cs = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
using(SqlConnection con = new SqlConnection(cs))
{
string itemCode = txtItemCode.Text;
string query =select ISNULL(ItemCode +'','')+'< br / > '+ ISNULL(名称+'','')作为Item,Isnull(左(Price,4),0)作为Y_ProductItem的价格,其中ItemCode ='+ @itemCode +';
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue(@ itemCode,itemCode);
con.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
while(dataReader.Read())
{
Session [Item] = dataReader [Item];
Session [Price] = dataReader [Price];
}

DataTable dt = new DataTable();
DataRow博士;
dt.Columns.Add(new System.Data.DataColumn(Rno,typeof(String)));
dt.Columns.Add(new System.Data.DataColumn(Item,typeof(String)));
dt.Columns.Add(new System.Data.DataColumn(Qty,typeof(String)));
dt.Columns.Add(new System.Data.DataColumn(Available,typeof(String)));
dt.Columns.Add(new System.Data.DataColumn(Price,typeof(String)));
dt.Columns.Add(new System.Data.DataColumn(Total,typeof(String)));
GridView1.DataSource = dt;
GridView1.DataBind();
if(Session [CurrentData]!= null)
{
for(int i = 0; i < < span class =code-attribute> rowcount + 1; i ++)

{

< span class =code-attribute> dt = (DataTable)会话[ CurrentData];
< span class =code-attribute>
< span class =code-attribute> < span class =code-attribute>

< span class =code-attribute> if (dt.Rows.Count > 0)
{



dr = dt.NewRow();




dr [0] = dt.Rows [0] [0] .ToString();
dr [1] = dt.Rows [0] [1] .ToString();
dr [2] = dt.Rows [0] [2] .ToString();
dr [3] = dt.Rows [0] [3] .ToString();
dr [4] = dt.Rows [0] [4] .ToString();
dr [5] = dt.Rows [0] [5] .ToString();



}
}
dr = dt.NewRow();
dr [0] = 1;
dr [1] = Convert.ToString(Session [Item]);
dr [2] = txtQuantity.Text;
// dr [3] = TextBox4.Text;
dr [4] = Convert.ToString(Session [Price]);
float q = Single.Parse(txtQuantity.Text);
float d = Single.Parse(Convert.ToString(Session [Price]));

dr [5] = d * q;
for(int r = 0; r < rowcount + 1; r ++)

{



dr [0] = r + 1;

}

dt.Rows.Add(dr);



}
< span class =code-attribute>
else

{

< span class =code-attribute> dr = dt.NewRow();

dr [0] = 1;

dr [1] = Convert.ToString(Session [ Item]);

< span class =code-attribute> dr [2] = txtQuantity.Text;

// dr [3] = TextBox4.Text;

dr[4] = Convert.ToString(Session[\"Price\"]); $b$ b
float q =Single.Parse(txtQuantity.Text);

float d = Single.Parse(Convert.ToString(Session[\"Price\"]));



dr[5] = d * q;

dt.Rows.Add(dr);



}



// If Session has a data then use the value as the DataSource

if (Session[\"CurrentData\"] != null)

{

GridView1.DataSource = (DataTable)Session[\"CurrentData\"];

GridView1.DataBind();

}

else

{

// Bind GridView with the initial data assocaited in the DataTable

GridView1.DataSource = dt;

GridView1.DataBind();



}

// Store the DataTable in Session to retain the values

Session[\"CurrentData\"] = dt;



}

}





protected void btnUpdate_Click(object sender, EventArgs e) 
{

// I want to implement here when update...

}

解决方案

You need to find the controls and fetch their values. Then you can update it.



Examples -

1. GridView Row Edit, Delete and Update[^]

2. Asp.net insert, Edit, update, delete data in gridview[^]


An EditItemTemplate will be an easy option for Updating the Gridview .



Refer

how-to-inserteditupdate-and-delete-data[^]


I was Implemented the gridView with datasource is dataTable. GridView contains textBox in templatefield.. My Question is I want to Update Gridview with values of textBox when i clicks the update button..

My GridView

<asp:GridView ID="GridView1" runat="server" Width="100%" CssClass="scrtablegrid" GridLines="None" 

                onrowdatabound="GridView1_RowDataBound" ShowFooter="True" 

                AutoGenerateColumns="False" onrowupdating="GridView1_RowUpdating" >
            <AlternatingRowStyle CssClass="scrgridbg2" />
            
            <HeaderStyle CssClass="scrgridheader" Height="30px" HorizontalAlign="Left" Font-Underline="true" ForeColor="White"/>
            <RowStyle CssClass="scrtablegrid" Height="40px" HorizontalAlign="Left" />
            <Columns>
                <asp:BoundField DataField="Rno" HeaderText="#" />
                <asp:TemplateField HeaderText="Item">
                     <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Qty">
                    
                    <ItemTemplate>
                    [<a href="#" class="removeqty">Remove</a>]<br />
		            <asp:TextBox ID="txtQty" runat="server" Text='<%# Bind("Qty") %>'	class="qtyinput" />
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Available" />
                <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:c}" />
                <asp:BoundField HeaderText="Total" DataField="Total" />
            </Columns>
            <FooterStyle CssClass="mitemstotalbg" Height="30px" HorizontalAlign="Left" />
        </asp:GridView>



Page behind Code

public void btnGo_Click(object sender, EventArgs e)
    {
        if (Session["CurrentData"] != null)
        {
            DataTable dt = (DataTable)Session["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
    
    }



private void BindGrid(int rowcount)
    {
        
        string cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            string itemCode = txtItemCode.Text;
            string query = "select  ISNULL(ItemCode+' ','')+'<br />'+ISNULL(Name+' ','') as Item,Isnull(left(Price,4),0) as Price  from Y_ProductItem where ItemCode='" + @itemCode + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@itemCode", itemCode);
            con.Open();
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                Session["Item"] = dataReader["Item"];
                Session["Price"] = dataReader["Price"];
            }

            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("Rno", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Item", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Qty", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Available", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Total", typeof(String)));
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (Session["CurrentData"] != null)
            {
                for (int i = 0; i < rowcount + 1; i++)

                {

                    dt = (DataTable)Session["CurrentData"];

                    

                    if (dt.Rows.Count > 0)
                    {

                        
                      
                        dr = dt.NewRow();
                      

                      

                        dr[0] = dt.Rows[0][0].ToString();
                        dr[1] = dt.Rows[0][1].ToString();
                        dr[2] = dt.Rows[0][2].ToString();
                        dr[3] = dt.Rows[0][3].ToString();
                        dr[4] = dt.Rows[0][4].ToString();
                        dr[5] = dt.Rows[0][5].ToString();


                       
                    }
                }
                dr = dt.NewRow();
                dr[0] = 1;
                dr[1] = Convert.ToString(Session["Item"]);
                 dr[2] = txtQuantity.Text;
                // dr[3] = TextBox4.Text;
                 dr[4] = Convert.ToString(Session["Price"]);
                 float q = Single.Parse(txtQuantity.Text);
                 float d = Single.Parse(Convert.ToString(Session["Price"]));

                 dr[5] = d * q;
                 for (int r = 0; r < rowcount + 1; r++)

                 {



                     dr[0] = r + 1;

                 }

                dt.Rows.Add(dr);



            }

            else

            {

                dr = dt.NewRow();

                dr[0] = 1;

                dr[1] = Convert.ToString(Session["Item"]);

                dr[2] = txtQuantity.Text;

                // dr[3] = TextBox4.Text;

                dr[4] = Convert.ToString(Session["Price"]);

                float q =Single.Parse(txtQuantity.Text);

                float d = Single.Parse(Convert.ToString(Session["Price"]));

               

                dr[5] = d * q;

                dt.Rows.Add(dr);



            }



            // If Session has a data then use the value as the DataSource

            if (Session["CurrentData"] != null)

            {

                GridView1.DataSource = (DataTable)Session["CurrentData"];

                GridView1.DataBind();

            }

            else

            {

                // Bind GridView with the initial data assocaited in the DataTable

                GridView1.DataSource = dt;

                GridView1.DataBind();



            }

            // Store the DataTable in Session to retain the values

            Session["CurrentData"] = dt;



        }

    }



protected void btnUpdate_Click(object sender, EventArgs e)
    {

       // I want to implement here when update...
        
    }

解决方案

You need to find the controls and fetch their values. Then you can update it.

Examples -
1. GridView Row Edit, Delete and Update[^]
2. Asp.net insert, Edit, update, delete data in gridview[^]


An EditItemTemplate will be an easy option for Updating the Gridview .

Refer
how-to-inserteditupdate-and-delete-data[^]


这篇关于如何更新DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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