如果在gridview的不同页面上,Gridview中的重复值

时间:2014-11-04 11:03:01

标签: c# asp.net gridview rowdatabound page-index-changed

我正在使用带有1个复选框列和3个其他列的Gridview。对于1个列我将在ButtonClick上的TextBox中添加值。

我希望重复的值不应显示在我在Rowdatabound中添加代码的gridview中。

当禁用分页时,代码可以正常工作但是当启用分页时,代码仅在将重复值添加到同一页面时才有效,但如果要添加的值是下一页,则接受该值。

protected void GVRequest_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //switch for first row 
            if (e.Row.RowIndex == 1)
            {
                GridViewRow Gprev = GVRequest.Rows[e.Row.RowIndex - 1];
                if (Gprev.Cells[1].Text.Equals(e.Row.Cells[1].Text))
                {
                    e.Row.Cells[1].Text = "";
                    e.Row.Visible = false;
                }
            }
            //now continue with the rest of the rows

            if (e.Row.RowIndex > 1)
            {
                //set reference to the row index and the current value
                int intC = e.Row.RowIndex;
                string lookfor = e.Row.Cells[1].Text;

                //now loop back through checking previous entries for matches 
                do
                {
                    GridViewRow Gprev = GVRequest.Rows[intC - 1];

                    if (Gprev.Cells[1].Text.Equals(e.Row.Cells[1].Text))
                    {
                        //e.Row.Cells[0].Text = "";
                        //e.Row.Cells[1].Text = "";
                        //e.Row.Cells[2].Text = "";
                        //e.Row.Cells[3].Text = "";
                        e.Row.Visible = false;
                        //Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('ID already added for processing');", true);
                    }

                    intC = intC - 1;
                } while (intC > 0);

            }

        }


    }



   protected void GVRequest_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        //DataRow drCurrentRow = null;

        GVRequest.PageIndex = e.NewPageIndex;
        GVRequest.DataSource = dtCurrentTable;
        //SetPreviousData();

        //GVRequest.DataBind();
        DataBind();
    }

//Add new Row
    private void AddNewRowToGrid()
    {
        if (ViewState["CurrentTable"] != null)
        {



            try
            {

                string RequestID = Request.QueryString["RequestID"];
                SqlConnection conn = new SqlConnection(GetConnectionString());
                StringBuilder sb = new StringBuilder(string.Empty);
                const string SQLFileRetrievalReq = "SELECT DeptFileID, FileStatus, FileID, RequestID FROM FileInwardMaster WHERE (FileID = @FileID) AND (RequestID = @RequestID)";
                SqlCommand cmdFileRetrievalReq = new SqlCommand(SQLFileRetrievalReq, conn);
                cmdFileRetrievalReq.Parameters.Add("@RequestID", SqlDbType.VarChar);
                cmdFileRetrievalReq.Parameters["@RequestID"].Value = RequestID;
                cmdFileRetrievalReq.Parameters.Add("@FileID", SqlDbType.VarChar);
                cmdFileRetrievalReq.Parameters["@FileID"].Value = TextBoxFileIds.Text.ToString();

                conn.Open();

                SqlDataReader reader = cmdFileRetrievalReq.ExecuteReader();

                reader.Read();

                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;

                drCurrentRow = dtCurrentTable.NewRow();
                //drCurrentRow["SlNo"] = dtCurrentTable.Rows.Count + 1;
                drCurrentRow["FileID"] = TextBoxFileIds.Text.ToUpper();
                drCurrentRow["RequestID"] = RequestID;
                drCurrentRow["DeptFileID"] = reader["DeptFileID"].ToString();

                dtCurrentTable.Rows.Add(drCurrentRow);

                ViewState["CurrentTable"] = dtCurrentTable;
                GVRequest.DataSource = dtCurrentTable;
                GVRequest.DataBind();
            }

            catch
            {

                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('ID Entered/Scanned not valid for Request. Please Enter/Scan a valid ID to proceed');", true);
                SetPreviousData();
                //SetInitialRow();
                //GVRequest.DataBind();

            }
        }

        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    //Get Previous Data
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["FileID"].ToString();// = txtRcptHdNo.Text;
                    dt.Rows[i]["RequestID"].ToString();// = ddlRcptDtlsItem.SelectedItem.Text;
                    dt.Rows[i]["DeptFileID"].ToString();// = ddlRcptDtlsItem.SelectedItem.Text;

                    rowIndex++;
                }
            }
        }

    }

    private void SetInitialRow()
    {
        string RequestID = Request.QueryString["RequestID"];

        DataTable dt = new DataTable();
        //DataRow dr = null;
        dt.Columns.Add(new DataColumn("RequestID", typeof(string)));
        dt.Columns.Add(new DataColumn("FileID", typeof(string)));
        dt.Columns.Add(new DataColumn("DeptFileID", typeof(string)));

       // dr = dt.NewRow();
       // dr["RequestID"] = string.Empty;
        //dr["FileID"] = string.Empty;
        //dr["DeptFileID"] = string.Empty;

        //dt.Rows.Add(dr);
        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;
        GVRequest.DataSource = dt;
        GVRequest.DataBind();

    }



    protected void BtnAddFileID_Click(object sender, EventArgs e)
    {            
        AddNewRowToGrid();
       //GenerateUniqueData(0);
        TextBoxFileIds.Text = "";            
    }

0 个答案:

没有答案