Yasa
Yasa

Reputation: 332

i need to hide a primary key when data on load but when edit i need to retrive

This method load the grid;

private void LoadData()
        {

            clsDataAccess objDAL = new clsDataAccess();
            DataTable DS = new DataTable();

            string objBLL = DDLTemTableList.SelectedValue.ToString();
            DS = objDAL.GetTemTableValue(objBLL);

            if (DS != null && DS.Rows.Count != 0)
            {
                lblNoRecord.Visible = false;

                foreach (DataColumn col in DS.Columns)
                {
                    //Declare the bound field and allocate memory for the bound field.
                    BoundField bfield = new BoundField();


                    //Initalize the DataField value.
                    bfield.DataField = col.ColumnName;


                    //Initialize the HeaderText field value.
                    bfield.HeaderText = col.ColumnName;


                    //Add the newly created bound field to the GridView.
                    GVDataEntry.Columns.Add(bfield);




                }


                GVDataEntry.DataSource = DS;
                GVDataEntry.DataBind();



            }
            else
            {
                lblNoRecord.Visible = true;
                GVDataEntry.DataSource = null;
                GVDataEntry.DataBind();

            }
        }

The GridView is loading perfect.but table all columns are loading i need to hide the first column(primary key) if i use mygrid.Columns[0].Visible = false; when i edit the gridview its not taken the key, so i am unable to update a table.

enter image description here

when i hide that primary column (in RedBox), i can not do edit(I can't change values)

Upvotes: 1

Views: 128

Answers (2)

user3497976
user3497976

Reputation: 21

u can try this

      protected void GVDataEntry_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                int index = GetColumnIndexByName(e.Row, "Row_Status");
                e.Row.Cells[index].Visible = false;
            }
        }

Upvotes: 1

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

If you are using those columns through BoundField then simply use this

<asp:BoundField DataField="Name_1" HeaderText="Name_1" Visible="false" />

Otherwise try below

   protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[index].Visible = false;
    }

Upvotes: 1

Related Questions