简体   繁体   中英

set gridview column uneditable in asp.net

I need to set gridview column uneditable. I also tried ((BoundField)gridView1.Columns[columnIndex]).ReadOnly = true; but it gives error of invalid index. I don't know where to use it. I used it in rowediting method.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        OleDbConnection conn;
        OleDbCommand cmd = new OleDbCommand();
        string path = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\website.mdb;Persist Security Info=False";
        conn = new OleDbConnection(path);
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = "select [email],[password] from users";
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }

UPDATED CODE: ASPX

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" Caption="User Info" CaptionAlign="Top" CellPadding="2" Font-Bold="True" ForeColor="Black" GridLines="None" ShowHeader="False" UseAccessibleHeader="False" Width="573px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"></asp:GridView>

Try like this:-

GridView1.Columns["ColumnName"].ReadOnly = true;

Hope it helps ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM