2
Reply

how to obtain the checked row values in a gridview from

jessica L

jessica L

Feb 18 2011 12:52 AM
2.8k

Hi experts,
I am currently doing a school project and I'll need to click a check field on the selected row and a [submit] button will trigger to another gridview which will show the detail of the product. Both the product and product details are separate tables in the database and have a foreign key of productID. I'll need to do a store procedure to INNER JOIN the 2 tables but I have no idea of how to do that using context.
Below is my aspx.cs :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.vrm.database;
using com.vrm.com;


public partial classProductSearch : System.Web.UI.Page
{
    private vrm_database vrmdb = new vrm_database();
    private String viewStateGVName = "gvProductSearchResult";

    //set the const for the ID of the GV Checkbox column
    private const string GVCHECKBOXCOLID = "selected";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            ViewState[this.ToString() + "_SortExpression"] = "productID";
            ViewState[this.ToString() + "_SortDirection"] = "ASC";
            btnSubmit.Visible = false;

        }
        if (IsPostBack)
        {
            btnSubmit.Visible = true;
        }

                 //DataTable tmpdt = vrmdb.Get_ProductSelection().Tables[0];
                 //tmpdt.PrimaryKey = new DataColumn[] { tmpdt.Columns[0] };
                 //ViewState[viewStateGVName] = tmpdt;

    }
    private void OpenPopUp()
    {
        throw new NotImplementedException();
    }
    protected void ClearButton_Click(object sender, EventArgs e)
    {
        gvProductSearchResult.Visible = false;
        tbproductID.Text = "";
        tbowner.Text = "";
        tbproductBrn.Text = "";
        tbproductName.Text = "";
        tbproductService.Text = "";
        tbstartDate.Text = "";
        tbendDate.Text = "";
        dropdownRange1.SelectedIndex = 0;
        dropdownRange2.SelectedIndex = 0;
        btnSearch.Visible = true;
        btnSubmit.Visible = false;

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGrid(true);
        gvProductSearchResult.Visible = true;
    }

    //Bind the GridView to with the Database returned records
    private void BindGrid(bool Reload)
    {
        string functionCode = "";

        //determine user access rights and show or hide edit button
        DataSet userAccessRights = vrmdb.Get_VRM_Permission(SessionHandler.UserLogin);


        if (userAccessRights.Tables[0].Rows.Count > 0)
        {
            functionCode = userAccessRights.Tables[0].Rows[0][0].ToString();
        }

        DataTable dtProductSelection  = null;
        if (Reload)
        {
            string OperatorStart = "=";
            string OperatorEnd = "=";

            if (dropdownRange1.SelectedValue == "Select One" || tbstartDate == null)
            {
                OperatorStart = "=";
            }
            else
            {
                OperatorStart = dropdownRange1.SelectedValue; ;
            }

            if (dropdownRange2.SelectedValue == "Select One" || tbendDate == null)
            {
                OperatorEnd = "=";
            }
            else
            {
                OperatorEnd = dropdownRange2.SelectedValue;
            }

            SqlDateTime startDate = SqlDateTime.Null;
            SqlDateTime endDate = SqlDateTime.Null;
            if (tbstartDate.Text != "")
            {
                startDate = SqlDateTime.Parse(tbstartDate.Text);
            }

             if (tbendDate.Text != "")
            {
                endDate = SqlDateTime.Parse(tbendDate.Text);
            }

 
          
            //Get from db and bind to datagrid
            dtProductSelection = vrmdb.Get_ProductSelection(tbproductID.Text,tbowner.Text,OperatorStart, startDate, OperatorEnd, endDate, tbproductBrn.Text, tbproductName.Text, tbproductService.Text).Tables[0];
                ViewState[viewStateGVName] = dtProductSelection;
        }
        else
        {
            //retrieve the ViewState object datatable from previous retrieval
            dtProductSelection = ViewState[viewStateGVName] as DataTable;
       
        }

        dtProductSelection.DefaultView.Sort = ViewState[this.ToString() +
              "_SortExpression"].ToString() + " " +
              ViewState[this.ToString() + "_SortDirection"].ToString();

        if (dtProductSelection != null)
       
        {
            if (dtProductSelection.Rows.Count > 0)
            {
                //gvProductSearchResult.Columns[GVCHKBOX].Visible = true;
                gvProductSearchResult.DataSource = ViewState[viewStateGVName];
                gvProductSearchResult.AllowSorting = true;
                gvProductSearchResult.DataBind();
                btnSearch.Visible = true;
            }
            else
            {
                dtProductSelection.Rows.Add(dtProductSelection.NewRow());

                ViewState[viewStateGVName] = dtProductSelection;
                gvProductSearchResult.AllowSorting = false;
                gvProductSearchResult.DataSource = ViewState[viewStateGVName];
                gvProductSearchResult.DataBind();
 
                //hide the checkbox and edit columns
                //gvProductSearchResult.Columns[selected].Visible = false;
                //gvProductSearchResult.Columns[GVEDITBTN].Visible = false;

                int TotalColumns = gvProductSearchResult.Rows[0].Cells.Count;
                gvProductSearchResult.Rows[0].Cells.Clear();
                gvProductSearchResult.Rows[0].Cells.Add(new TableCell());
                gvProductSearchResult.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                gvProductSearchResult.Rows[0].Cells[0].Text = "No Record Found";

                btnSearch.Visible = true;
            }
        }
    }

    private void GetSortDirection(string sColumn)
    {
        //set sort direction to asc
        string sSortDirection = "ASC";
        string sSortExpression = ViewState[this.ToString() +
                "_SortExpression"] as string;

        if (sSortExpression != null)
        {
            //check same column is being sorted

            if (sSortExpression == sColumn)
            {
                string sLastDirection = ViewState[this.ToString() +
                          "_SortDirection"] as string;
                if ((sLastDirection != null) && (sLastDirection == "ASC"))
                {
                    sSortDirection = "DESC";
                }
            }
        }

        //save new values in view
        ViewState[this.ToString() + "_SortDirection"] = sSortDirection;
        ViewState[this.ToString() + "_SortExpression"] = sColumn;
    }

protected void btnSubmit_Click(object sender, EventArgs e)
{
//if items are selected
Boolean error = false;
DataTable checkedItems = GridViewCommon.GetCheckedItems(GVCHECKBOXCOLID, gvProductSearchResult, ViewState[viewStateGVName] as DataTable);
if (checkedItems != null)
{
if (checkedItems.Rows.Count > 0)
{
Context.Items["SelectedProductRecord"] = checkedItems;
Server.Transfer("ProductDetail.aspx");
}
else
{
error = true;
}
}
if (error == true)
{
showMessage("Please select at least ONE product");
}
 
}
    private void showMessage(string message)
    {

        string messageBoxScript = "<script> window.onload = function(){alert('" + message + "');}";
        messageBoxScript += "</" + "script>";
        Response.Write(messageBoxScript);
    }

    protected void gvProductSearchResult_Sorting(object sender, GridViewSortEventArgs e)
    {      
        GetSortDirection(e.SortExpression);
        BindGrid(true);
    }
    protected void selected_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk;
        foreach (GridViewRow rowItem in gvProductSearchResult.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("selected"));
            chk.Checked = ((CheckBox)sender).Checked;
        }
    }
}

 
Hope anyone could help me in this.
Thanks!

Answers (2)