November 8, 2011

Set Auto generated columns as read only in Editable Grid View

Below code can be used to make read only columns in grid view


  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        rowIndex = e.NewEditIndex;


        BindData();
        FillGrid();
    }



//Make 1,2,3,4,7,8,9 as readonly

  protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (rowIndex > -1)
        {
            if (GridView1.Rows.Count > rowIndex)
            {
                if (GridView1.Rows[rowIndex].Cells.Count > 1)
                {
                    if (GridView1.Rows[rowIndex].Cells[1].Controls.Count > 0)
                    {
                        for (int i = 1; i < 4; i++)
                        {
                            if (GridView1.Rows[rowIndex].Cells[i].Controls[0].GetType() == typeof(TextBox))
                            {
                                ((TextBox)GridView1.Rows[rowIndex].Cells[i].Controls[0]).Visible = false;
                                GridView1.Rows[rowIndex].Cells[i].Text
                                    = ((TextBox)GridView1.Rows[rowIndex].Cells[i].Controls[0]).Text;
                            }
                        }
                        for (int j = 7; j < 10; j++)
                        {
                            if (GridView1.Rows[rowIndex].Cells[j].Controls[0].GetType() == typeof(TextBox))
                            {
                                ((TextBox)GridView1.Rows[rowIndex].Cells[j].Controls[0]).Visible = false;
                                GridView1.Rows[rowIndex].Cells[j].Text
                                    = ((TextBox)GridView1.Rows[rowIndex].Cells[j].Controls[0]).Text;
                            }
                        }






                    }
                }
            }
        }
    }

1 comment:

  1. when click on Edit the value of coluamn should be readonly,how to do?

    ReplyDelete