从嵌套网格视图内的网格视图复制数据 [英] Copy data from a gridview inside a nested gridview

查看:89
本文介绍了从嵌套网格视图内的网格视图复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有三个Gridview,即A,B和C.



Gridview C已放置在GridView B内的模板字段中。



我们想在按钮点击时将GridView A中的数据复制到GridView C中。



我们试图使用相同的数据表但无法获得理想的结果。



实际上,我们正在创建一个产品订购页面,其中税项适用于项目。对于每个项目,将适用不同的税种。我们想要显示与gridview行中每个项目相对应的税。



以下是我们的代码: -



DataTable dt1 = new DataTable();



We are having three Gridviews namely A,B and C.

Gridview C has been placed in a template field inside GridView B.

We want to copy data from GridView A into GridView C on button click.

We have tried to use datatable for the same but unable to get desired result.

Actually we are creating a product ordering page in which tax is applicable item wise. For each item there will be different set of taxes applicable. We want to show taxes corresponding to each item in a gridview row.

Following is our code:-

DataTable dt1 = new DataTable();

protected DataTable Tax()
   {
       DataTable dt2 = new DataTable();
       dt2.Columns.Add("taxType", typeof(string));
       dt2.Columns.Add("taxPercentage", typeof(string));

       for(int i=0;i<GridViewA.Rows.Count;i++)
       {
           DataRow dr = dt2.NewRow();
           dr["taxType"] = ((Label)GridViewA.Rows[i].Cells[0].FindControl("LabelChargeName")).Text;
           dr["taxPercentage"] = ((DropDownList)GridViewA.Rows[i].Cells[1].FindControl("ddlTaxPercentage")).SelectedValue ;
           dt2.Rows.Add(dr);
           dt2.AcceptChanges();
       }

       return dt2;


   }







private void gridVIEWData()
    {
        dt1.Columns.Add("itemCode", typeof(string));
        dt1.Columns.Add("itemShrtName", typeof(string));
        dt1.Columns.Add("itemName", typeof(string));
        dt1.Columns.Add("itemQuantity", typeof(string));
        dt1.Columns.Add("itemRate", typeof(string));
        dt1.Columns.Add("itemTax", typeof(DataTable));
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if (ViewState["Cart"] == null)
        {
            gridVIEWData();
        }
        else
        {
            dt1 = (DataTable)ViewState["Cart"];
        }
            DataRow dr = dt1.NewRow();
            dr["itemCode"] = LabelItemCode.Text;
            dr["itemShrtName"] = LabelItemShrtName.Text;
            dr["itemName"] = ddlProduct.SelectedItem;
            dr["itemQuantity"] = TextBoxPOQuantity.Text;
            dr["itemRate"] = TextBoxPORate.Text;
        
// Here we are copying datatable result in datarow of another datatable

            dr["itemTax"] = Tax();

            dt1.Rows.Add(dr);
            dt1.AcceptChanges();
            ViewState["Cart"] = dt1;
            GridView1.DataSource = dt1;
            GridView1.DataBind();
   
    }
    protected void GridViewB_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataTable dt = (DataTable)ViewState["Cart"];
        DataRow dr;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            dr = dt.Rows[0];
            GridView GridTax = (GridView)e.Row.Cells[5].FindControl("GridViewC");
            GridTax.DataSource =(DataTable)dr["itemTax"];
            GridTax.DataBind();

     }
    }

推荐答案

参考


这篇关于从嵌套网格视图内的网格视图复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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