She Must Click the Enable Editing Button to Continue

Actually, if you just want a clean grid? Then dump's ALL of the templates, don't use them. In fact, you find the whole setup is a LOT less mess, and often less code, and MUCH less markup.

In fact, because you have to place each plane jane asp.net control (columns) in the GV as a "template" field, I often use a listview. (because it lets you drop in controls for each column(s), and no messy "item template" stuff is required - which gets tired VERY fast).

And worse yet? Why have some enable edit button? Why torture the users and clutter up the UI? Why not just display the grid. User can tab around, edit and change any row. (kind of like excel). At that point, put below the GV a save edit buttion, and maybe a un-do button. Once again, you MASSIVE reduce the UI work load on the user - and you also elmonate a BOATLOAD of UI buttons and all kinds of confusing jazz for the user. Just toss up a grid. Let user edit anywhere, tab around, and change what they way. When done, they can click the final save button below the gv, adn your done. As noted, this is less code, less markup, less confusing to the user, and really far more user friendly to boot.

So, as I stated, for a few columns, GV is ok, but if you have lots of columns, then I STRONG suggest flipping over to list view, since as noted you eliminate the messy need and silly requirement for surrounding each asp.net control inside of the "item template" required for each control.

Ok, so lets say we have this Grid markup:

                      <div style="width:60%;padding:25px">     <style> .borderhide input, textarea {border:none}</style>      <asp:GridView ID="GVHotels" runat="server" class="table" AutoGenerateColumns="false" DataKeyNames="ID"         CssClass="table table-hover borderhide" >       <Columns>         <asp:TemplateField  HeaderText="HotelName" >             <ItemTemplate><asp:TextBox id="HotelName" runat="server" Text='<%# Eval("HotelName") %>'                Width="160px"/></ItemTemplate>         </asp:TemplateField>          <asp:TemplateField  HeaderText="FirstName">             <ItemTemplate>                 <asp:TextBox id="FirstName" runat="server" Text='<%# Eval("FirstName") %>' Width="100px"/>             </ItemTemplate>         </asp:TemplateField>          <asp:TemplateField  HeaderText="LastName" >             <ItemTemplate><asp:TextBox id="LastName" runat="server" Text='<%# Eval("LastName") %>'  Width="100px"/></ItemTemplate>         </asp:TemplateField>          <asp:TemplateField  HeaderText="City" >             <ItemTemplate><asp:TextBox id="City" runat="server" Text='<%# Eval("City") %>' Width="100px" /></ItemTemplate>         </asp:TemplateField>                          <asp:TemplateField     HeaderText="Description" >             <ItemTemplate><asp:TextBox id="Description" runat="server"                  Text='<%# Eval("Description") %>'  Width="250px" TextMode="MultiLine"                 Style="overflow:hidden"  /></ItemTemplate>         </asp:TemplateField>                          <asp:TemplateField     HeaderText="Active"  ItemStyle-HorizontalAlign="Center">             <ItemTemplate>            <asp:CheckBox ID="chkActive" Checked='<%# Eval("Active") %>' runat="server" />            </ItemTemplate>         </asp:TemplateField>              </Columns>         </asp:GridView>         <br />         <asp:Button ID="cmdSave" runat="server" Text="Save Edits" CssClass="btn" OnClick="cmdSave_Click" />                  

So, note how we JUST have a grid - plane jane. As noted, we don't even need a edit template or anything:

Code to fill this grid is thus:

                      DataTable rstData = new DataTable();     protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {             LoadGrid();             ViewState["rstData"] = rstData;         }         else             rstData = ViewState["rstData"] as DataTable;     }      void LoadGrid()     {         using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))         {             string strSQL = "SELECT * FROM tblHotels ORDER BY HotelName";             using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))             {                 conn.Open();                 rstData.Load(cmdSQL.ExecuteReader());                 GVHotels.DataSource = rstData;                 GVHotels.DataBind();             }         }     }                  

And now we have this:

enter image description here

Ok, so we should add a un-do button, and probablly a add row button.

So, all we have so far is the save button, and that code is this:

                      protected void cmdSave_Click(object sender, EventArgs e)     {         // send grid back to rstData         GridToTable();         // now send table back to database with updates         string strSQL = "SELECT * from tblHotels";         using (SqlCommand cmdSQL = new SqlCommand(strSQL,                                     new SqlConnection(Properties.Settings.Default.TEST4)))         {             cmdSQL.Connection.Open();             SqlDataAdapter daupdate = new SqlDataAdapter(cmdSQL);             SqlCommandBuilder cmdBuild = new SqlCommandBuilder(daupdate);              daupdate.Update(rstData);         }     }       void GridToTable()     {         // pull grid rows back to table.         foreach (GridViewRow rRow in GVHotels.Rows)         {             int RecordPtr = rRow.RowIndex;             DataRow OneDataRow;             OneDataRow = rstData.Rows[RecordPtr];             OneDataRow["FirstName"] = ((TextBox)rRow.FindControl("FirstName")).Text;             OneDataRow["LastName"] = ((TextBox)rRow.FindControl("LastName")).Text;             OneDataRow["HotelName"] = ((TextBox)rRow.FindControl("HotelName")).Text;             OneDataRow["City"] = ((TextBox)rRow.FindControl("City")).Text;             OneDataRow["Description"] = ((TextBox)rRow.FindControl("Description")).Text;             OneDataRow["Active"] = ((CheckBox)rRow.FindControl("chkActive")).Checked;         }     }                  

So, as you can see, we get full editing of any row on the grid. There is no silly edit button, no silly edit templates. In fact, no real silly anything.

So, just tab around - make changes to anything in the grid, and then hit save button and you done. And users will LOVE this, since when they edit text, or even Excel etc., they just edit, play, have fun, and when done hit "save", so your users will love your you for this UI, since that's what they use and do all day long.

rudolphfrony1946.blogspot.com

Source: https://stackoverflow.com/questions/70956566/enable-editing-in-gridview-on-button-click-outside-of-gridview

0 Response to "She Must Click the Enable Editing Button to Continue"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel