Small example in GridView of dynamically setting whether CommandField is available or visible

  • 2020-06-07 04:23:29
  • OfStack


 protected void gvMaterial_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;

            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (gvMaterial.PageIndex * gvMaterial.PageSize + e.Row.RowIndex + 1).ToString();
                string price;
                try // Make the edit function available under certain conditions 
                {
                     price = ((TextBox)e.Row.Cells[4].Controls[0]).Text;// Click edit 
                }
                catch
                {
                     price = e.Row.Cells[4].Text;    // In the beginning 
                }                          
                if (price == "" || price == " ")
                {
                    e.Row.Cells[7].Controls[0].Visible = false;
                }
                else
                {
                    e.Row.Cells[7].Controls[0].Visible = true;
                }
            }
        }
 

Related articles: