Hi
Is is possible to force Grid view edit mode without using object data source, I set EditIndex, but the Edit template is never shown till I change the binding to ObjectDataSource
Thanks
Hi
Is is possible to force Grid view edit mode without using object data source, I set EditIndex, but the Edit template is never shown till I change the binding to ObjectDataSource
Thanks
Do you mean to set Edit-Mode as default? Because you have to databind your gridview, otherwise there are no items to show.
I don't know if you mean such an example but I hope it will be helpful:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit">
<Columns>
<asp:CommandField EditText="Edit" UpdateText="Update" ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
}
class Item
{
public string Name { get; set; }
}
class ItemRepo
{
public static List<Item> GetItemList()
{
List<Item> list = new List<Item>();
list.Add(new Item() { Name = "Item1" });
list.Add(new Item() { Name = "Item2" });
list.Add(new Item() { Name = "Item3" });
list.Add(new Item() { Name = "Item4" });
list.Add(new Item() { Name = "Item5" });
return list;
}
}
I think your problem can be not databinding the gridview after setting editindex.