I am still learning about using gridviews and as it is, I have an access database that consists of three tables(as part of the exercise) and display the values fields from all three tables using a join statement.
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
Data Source =" + Server.MapPath("App_Data\\Shopping List.mdb"));
conn.Open();
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('a connection was established with the database')", true);
DataSet ds = new DataSet();
OleDbDataAdapter dba = new OleDbDataAdapter(@"SELECT Ingredient.IngredientId, Ingredient.IngredientName, Area.AreaName, Recipe.RecipeName, Ingredient.Quantity
FROM (Ingredient
INNER JOIN Area ON Ingredient.AreaId = Area.AreaId)
INNER JOIN Recipe ON Ingredient.RecipeId = Recipe.RecipeId", conn);
dba.Fill(ds);
gridIngredients.DataSource = ds;
gridIngredients.DataBind();
I now added a the edit button by setting the autoGenerateEditButton to true. How would I go about in adding a textbox or dropdownlist to a row to update the data?
<asp:GridView ID="gridIngredients" runat="server" AutoGenerateColumns="False"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" ForeColor="Black" GridLines="None"
AutoGenerateEditButton="True"
onselectedindexchanged="gridIngredients_SelectedIndexChanged"
Width="543px" onrowediting="gridIngredients_RowEditing">
<Columns>
<asp:BoundField DataField="IngredientId" HeaderText="ID" />
<asp:BoundField DataField="IngredientName" HeaderText="Ingredient" />
<asp:BoundField DataField="AreaName" HeaderText="Area" />
<asp:BoundField DataField="RecipeName" HeaderText="Recipe" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
</Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
regards
For readonly fields, like IngredientId, add a ReadOnly=”True”
For fields edited by text box, like Quantity.
For special editors, like dropdownlist or jQuery plugin, use TemplateField to customize your EditItemTemplate(For edit) and ItemTemplate(For view)