我还在学习使用gridviews,因为它有一个包含三个表的访问数据库(作为练习的一部分),并使用join语句显示所有三个表中的values字段。
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();
我现在通过将autoGenerateEditButton设置为true来添加编辑按钮。如何在行中添加文本框或下拉列表来更新数据?
<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>
问候
答案 0 :(得分:1)
您可以按如下方式修改Columns
部分:
<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" />
<asp:TemplateField>
<HeaderTemplate>
A custom column
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="textBox" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
答案 1 :(得分:1)
对于只读字段,例如IngredientId,添加ReadOnly =“True” 对于按文本框编辑的字段,如“数量”。 对于特殊编辑器,如dropdownlist或jQuery插件,使用TemplateField自定义EditItemTemplate(用于编辑)和ItemTemplate(用于视图)
<asp:BoundField DataField="IngredientId" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"/>
<asp:TemplateField HeaderText="Area">
<EditItemTemplate>
<asp:DropDownList ID="DDLArea" runat="server"
Text='<%# Bind("AreaCode") %>'>
<!--items-->
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("AreaName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>