locked
How to hide the column from the datatable? RRS feed

  • Question

  • Dear All, 

    May i know how to hide a row in gridview ?

    For the example, 

    i want hide 

    dt.Columns.Add(new System.Data.DataColumn("DATE", typeof(String)));


    private void BindGrid(int rowcount)
            {
    
                DataTable dt = new DataTable();
    
                DataRow dr;
    
                dt.Columns.Add(new System.Data.DataColumn("DATE", typeof(String))); // 0
                dt.Columns.Add(new System.Data.DataColumn("ITEM", typeof(String))); // 1
                dt.Columns.Add(new System.Data.DataColumn("TYPE OF WASTE", typeof(String))); //2 
                dt.Columns.Add(new System.Data.DataColumn("Description", typeof(String)));//3
                dt.Columns.Add(new System.Data.DataColumn("Material", typeof(String))); // 4
                dt.Columns.Add(new System.Data.DataColumn("NET WEIGHT", typeof(String)));// 5
                dt.Columns.Add(new System.Data.DataColumn("Document Number", typeof(String))); // 6
                dt.Columns.Add(new System.Data.DataColumn("STATUS", typeof(String))); // 7
                dt.Columns.Add(new System.Data.DataColumn("CONTAINER QUANTITY", typeof(String))); //8
                dt.Columns.Add(new System.Data.DataColumn("CONTAINER TYPE", typeof(String))); // 9
                dt.Columns.Add(new System.Data.DataColumn("CONTAINER SOURCE", typeof(String))); //10
                dt.Columns.Add(new System.Data.DataColumn("LOCATOR", typeof(String))); //11
                dt.Columns.Add(new System.Data.DataColumn("REASON CODE", typeof(String))); //12
                dt.Columns.Add(new System.Data.DataColumn("VENDOR NAME", typeof(String))); //13
                dt.Columns.Add(new System.Data.DataColumn("REMARK", typeof(String))); //14
                dt.Columns.Add(new System.Data.DataColumn("CREATEUSER", typeof(String))); //15
                dt.Columns.Add(new System.Data.DataColumn("CREATEDATE", typeof(String))); //16
    
    
    
                if (ViewState["CurrentData"] != null)
                {
    
                    for (int i = 0; i < rowcount + 1; i++)
                    {
    
                        dt = (DataTable)ViewState["CurrentData"];
    
                        if (dt.Rows.Count > 0)
                        {
    
                            dr = dt.NewRow();
    
                            dr[0] = dt.Rows[0][0].ToString();
    
                            
    
                        }
    
                    }
    
                    dr = dt.NewRow();
    
                    dr[0] = WMMRDATE.Text;
                    dr[1] = WMMRItem.SelectedValue;
                    dr[2] = WMMRItemType.SelectedValue;
                    dr[3] = WMMRDesc.Text;
                    dr[4] = WMMRMaterial.SelectedValue;
                    dr[5] = WMMRNetWeight.Text;
                    dr[6] = WMMRDocNum.Text;
                    dr[7] = WMMRStatus.Text;
                    dr[8] = WMMRContQty.Text;
                    dr[9] = WMMRContType.Text;
                    dr[10] = WMMRContSource.Text;
                    dr[11] = WMMRLoc.Text;
                    dr[12] = WMMRReason.Text;
                    dr[13] = WMMRVendor.SelectedValue;
                    dr[14] = WMMRRemark.Text;
                    dr[15] = WMMRCUSER.Text;
                    dr[16] = WMMRCDATE.Text;
    
    
    
    
                    dt.Rows.Add(dr);
    
    
    
                }
    
          else
               {
    
                  dr = dt.NewRow();
    
                  dr[0] = WMMRDATE.Text;
                  dr[1] = WMMRItem.SelectedValue;
                  dr[2] = WMMRItemType.SelectedValue;
                  dr[3] = WMMRDesc.Text;
                  dr[4] = WMMRMaterial.SelectedValue;
                  dr[5] = WMMRNetWeight.Text;
                  dr[6] = WMMRDocNum.Text;
                  dr[7] = WMMRStatus.Text;
                  dr[8] = WMMRContQty.Text;
                  dr[9] = WMMRContType.Text;
                  dr[10] = WMMRContSource.Text;
                  dr[11] = WMMRLoc.Text;
                  dr[12] = WMMRReason.Text;
                  dr[13] = WMMRVendor.SelectedValue;
                  dr[14] = WMMRRemark.Text;
                  dr[15] = WMMRCUSER.Text;
                  dr[16] = WMMRCDATE.Text;
                
    
    
    
                   dt.Rows.Add(dr);
    
    
    
               }
    
    
    
                // If ViewState has a data then use the value as the DataSource
    
                if (ViewState["CurrentData"] != null)
                {
    
                    GridView1.DataSource = (DataTable)ViewState["CurrentData"];
    
    
                    GridView1.DataBind();
    
                   
    
                }
    
                else 
                {
    
                   
    
                    // Bind GridView with the initial data assocaited in the DataTable
    
                    GridView1.DataSource = dt;
    
                    GridView1.DataBind();
    
    
    
                }
    
                // Store the DataTable in ViewState to retain the values
    
                ViewState["CurrentData"] = dt;
    
    
    
            }
    
    
    
    
            protected void WMAdd_Click(object sender, EventArgs e)
            {
    
                GridViewMR.Visible = true;
    
                // Check if the ViewState has a data assoiciated within it. If
                if (ViewState["CurrentData"] != null)
                {
                   
                    DataTable dt = (DataTable)ViewState["CurrentData"];
                    int count = dt.Rows.Count;
                    BindGrid(count);
                  
                }
                else
                {
                    BindGrid(1);
                }
    
               
                
                 WMMRNetWeight.Text = string.Empty;
                 WMMRDocNum.Text =  string.Empty;
                 WMMRContQty.Text = string.Empty;    
                 WMMRRemark.Text = string.Empty;
    
             
    
             
                WMMRNetWeight.Focus();
                WMMRDocNum.Focus();
                WMMRContQty.Focus();
                WMMRRemark.Focus();
            
               
            }

    Wednesday, September 4, 2013 12:54 AM

Answers

  • Hi, you can:

    1) Hide the column by using GridView1.Columns[0-based index].Visible = false after calling GridView's DataBind()

    2) Handle the event of GridView's RowDataBound, and then hide the Cell by e.Row.Cells[0-based index].Visible = false, you should check whether the current RowType is Data or not:

    void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
            
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
                e.Row.Cells[0-based index].Visible = false;
        }
        
      }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    • Proposed as answer by Wizend Wednesday, September 4, 2013 4:31 PM
    • Marked as answer by Caillen Thursday, September 12, 2013 1:04 PM
    Wednesday, September 4, 2013 1:27 AM