Hi,

Yes you can disable a cell. But the disabling makes a difference only if you have some server control like a text box in the cells of the grid view. I can demonstrate this with a couple of examples. First I will show you an ordinary grid view application called Sample1. What this application does is to create a Grid View with 3 rows and 3 columns and in all the 3 rows the first column contains "0" and the second column contains "1" and the third column contains "2". Then the code disables the second cell in each row which contains "1". Open a fresh website in VS 2005. You will get a web page by default called Default.aspx. I am giving the code below for the html view as well as the code behind page. Just substitute the code that appears by default when you open the application with what I give below

Here is the code for application Sample1

Html code for Default.aspx of Sample1

Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="mygridview" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>
And here is code for Default.aspx.cs of Sample1

Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        /* Create the data table which you are going to bind to the Gridview.
         * After executing this code the data table contains 3 rows and 3 columns
         * And in all three rows the first column contains "0" the scond contains 
         * "1" and the third contains "2" */
        
        DataTable dt = new DataTable();
        dt.Columns.Add("Column0", System.Type.GetType("System.String"));
        dt.Columns.Add("Column1", System.Type.GetType("System.String"));
        dt.Columns.Add("Column2", System.Type.GetType("System.String"));

        for (int i = 0; i < 3; i++)
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            for (int j = 0; j < 3; j++)
            {
                int p = j;
                dt.Rows[i][j] = p.ToString();
            }
        }


        mygridview.DataSource = dt.DefaultView;
        mygridview.DataBind();

        /* Code for disabling all cells that contain the numeral "1"
         * Code will execute perfectly but the disabling will have no effect because 
         * the cells of the gridview do not contain any controls */

        for (int i = 0; i < mygridview.Rows.Count; i++)
        {
            GridViewRow g = mygridview.Rows[i];
            for(int j = 0; j < g.Cells.Count; j++)
            {
                if(g.Cells[j].Text == "1")
                    g.Cells[j].Enabled = false;
            }
        }
    }
}
Now if you create this application and run it you will find that the code executes perfectly but the grid just appears normally and there is not much meaning in disabling the cells that contain the numeral "1".

Now I will show you the second application called Sample2 where I create a grid view which contains text boxes in each cell with the same numerals as in the previous sample and the second column of each row which contains "1" gets disabled. This means the text box in the second column of each row gets disabled and you cannot type anything into those text boxes whereas in other text boxes you can type something in. In this case there is a meaning in disabling the cells of a grid view. You can open another fresh application and copy the code given below for the default page.

Code for application Sample2

Html code for Default.aspx of Sample2

Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="mygridview" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column0") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column1") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Column2") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>
And here is the code for Default.aspx.cs of Sample2

Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        /* Create the data table which you are going to bind to the Gridview.
         * After executing this code the data table contains 3 rows and 3 columns
         * And in all three rows the first column contains "0" the scond contains 
         * "1" and the third contains "2" */
        
        DataTable dt = new DataTable();
        dt.Columns.Add("Column0", System.Type.GetType("System.String"));
        dt.Columns.Add("Column1", System.Type.GetType("System.String"));
        dt.Columns.Add("Column2", System.Type.GetType("System.String"));

        for (int i = 0; i < 3; i++)
        {
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            for (int j = 0; j < 3; j++)
            {
                int p = j;
                dt.Rows[i][j] = p.ToString();
            }
        }


        mygridview.DataSource = dt.DefaultView;
        mygridview.DataBind();

        /* Code for disabling all cells that contain the numeral "1"
         * Code will execute perfectly and the text boxes in the second column that
         * contain the numeral "1" get diabled and you cannot type anything
         * in them whereas you can type something in the other 
         * text boxes */

        for (int i = 0; i < mygridview.Rows.Count; i++)
        {
            GridViewRow g = mygridview.Rows[i];
            for(int j = 0; j < g.Cells.Count; j++)
            {
                TextBox t = (TextBox)mygridview.Rows[i].Cells[j].Controls[1];
                if (t.Text == "1")
                    t.Enabled = false;
            }
        }
    }
}
If you run the second application you will see what I mean.

Hope this helps.

Warm Regards

Quote Originally Posted by vuyiswam View Post
Good Afternoon guys

Is it Possible in a Gridview to Disable a Cell like a button ?

I have been Googling for this but no luck

Thank you