将复选框添加到现有的gridview [英] add checkbox to a exsisting gridview

查看:76
本文介绍了将复选框添加到现有的gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我创建了一个包含joblist的grideview。下面我添加了一个datadetailview。当任何人选择gridview中的行时,它将在详细视图中显示...我在detailview中添加了一个列复选框。当有人选中复选框并单击在下方应用按钮,必须将详细视图中的数据添加到另一个应用的表中。我们可以这样做吗?



Here I created a grideview containging joblist.below that i added one datadetailview .when anybody select the row in gridview it will shown in detail view...I added one column checkbox in the detailview.when anbody select the checkbox and click apply button below ,the data in the detailview must be added to another table applied jobs.how can we do that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class joblist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName"), new DataColumn("Designation") });
            dt.Rows.Add("NIIT", "programmer");
            dt.Rows.Add("annvision", "Webdesigner");
            dt.Rows.Add("softtech", "designer");
            dt.Rows.Add("Mahad", "Programmer");
            DetailsView1.DataSource = dt;
            DetailsView1.DataBind();
        }
  
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/secure.aspx");
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (GridView1.SelectedRow == null)
        {
            DetailsView1.Visible = false;
        }
        else
        {
            DetailsView1.Visible = true;
        }
    }
    
    protected void Apply_Click(object sender, EventArgs e)
    {
        Response.Write("You are successfully applied");
        
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("CompanyName"), new DataColumn("Designation") });
        foreach (DetailsViewRow row in DetailsView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                if (chkRow.Checked)
                {
                    string CompanyName = row.Cells[1].Text;
                    string Designation = (row.Cells[2].FindControl("lblDesignation") as Label).Text;
                    dt.Rows.Add(CompanyName, Designation);
                }
            }
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();

    }


}



它在detailview1.DataBind()附近显示错误;

错误:用户代码未处理无效的OperationException

DataSource和DataSourceID都在DetailsView1上定义。删除一个定义。


it showing error near detailview1.DataBind();
error:Invalid OperationException was unhandled by usercode
Both DataSource and DataSourceID are defined on 'DetailsView1'. Remove one definition.

推荐答案

您只需要将Datasource或DatasourceID绑定到DetailsView1控件。



检查DetailsView1的声明部分(HtmlMarkup)是否在那里设置了DatasourceID。删除其中一个,声明部分上的DatasourceID或后面代码中设置的DataSource。
You only require to have Datasource or DatasourceID bound to the DetailsView1 control.

Check the declarative part too ( HtmlMarkup) of DetailsView1 whether DatasourceID is set there. Remove one of these, either DatasourceID on declarative part or DataSource set inthe code behind.


这篇关于将复选框添加到现有的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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