Hey- I am having trouble with a simple datagrid example, and need some advice if anyone is knowledgable. It's really driving me batty! I've searched newsgroups and help sites but nobody seems to have an answer that makes things work, although lots of folks seem to have had this problem before.. It's funny how much time you can waste when you think the answer is just around the corner! Setup: 1 I have an ASP.NET page with a datagrid, autogenerate columns is true, viewstate is enabled, and the DG has an Edit/Update/Cancel button. 2. In Page_Load, I load up the DataGrid with data and bind it, only if postback is false 3. In the EditCommand event, I set the edit item index to the row selcted. I do not databind here. Problem: I have to click the edit link twice to make the row editable. If I click once the page appears unchanged. Since I CAN get the page to reload with the selected row editable after two clicks, I figure that the DataGrid is saving the data to the viewstate. So, I should not need to reload from the database or save the dataset in the session or suchlike hack. I mean, that is what the viewstate is for, right? It obviously knows how to refresh the DataGrid from the viewstate. But, if I call DataBind on the DataGrid again in the EditCommand method, all the data in the grid dissapears (as I would expect, since it has no datasource defined when I bind). I'm almost thinking to tell my users to just click twice! Does anyone have any ideas or suggestions (Besides "Enable Viewstate" or "data bind in the Edit command method" or "store the dataset to the session"?) Thanks so much, I am delerious and heading off to bed now! Diane
I got home last night and had a flash of inspiration, and I thought I knew how to fix things! One, it's pretty obvious that the edit item command is working, but since the click even happens after the datagrid is fetched from the viewstate, the editability doesn't show up til the next page load, when the grid is bound again. So, I tried using server.transfer to force the page to reload. Unfortunately, this sets up an infinite loop. Sigh. Later, I tried dragging a dataset to the form in design view instead of creating the dataset in code. This adds some promising looking code in initializecomponent, but no, things still work the same. If only I could figure out a way to force the page to reload AFTER the editcommand event, so the editability of the grid would show without having to click again. Sigh. Having a conversation with yourself is a sure sign you are insane! Diane
OMG! I found the answer! It works! In your EditCommand event handler, you need to call the DataBind of the page: this.DataGrid1.EditItemIndex = e.Item.ItemIndex; this.DataBind(); If you call the databind of the DataGrid, all your data dissapears. But databind of the Web Form, it works. So to recap for all you Google searchers: If you have to click twice on your datagrid to show the editable row when the user clicks the edit button, add a call the the DataBind of your Page (not the DataGrid) in the editcommand event handler My, it feels good to get that working. And, I only am going to the database once! Woot woot woot woot woot! Diane
Oh, I almost forgot. The other thing that is kind of spooky and essential: You need to drag the dataset and connection and adapter on to the web form, you can;t just create them in code. I think it may only be the adapter and dataset that need to be created via the toolbox, not the connection. I have sitting in front of me right now two pages. One, I create my dataset and adapter and connection in page load and fill the datagrid. Two, I dragged the dataset and adapter and connection to the page, so they are instantiated for me in initializecomponent. Page One - datagrid dissapears when I click edit Page Two - Datagrid date persists between page loads Anyway, I will probe and post not furthur. Hope this helps someone else in the future. You gotta drag those babies to the page!
Oh misery. I have tricked myself. It's still broken. I forgot to only load the data on postback = = false. It's still broken. Woe is me, the world is gray and all joy is gone from living. It's so tantalizing, because the page almost works well, excpet for the pesky double-click. Sigh My Code: <PRE> private void Page_Load(object sender, System.EventArgs e) { if (this.IsPostBack == false) { sqlConnection1.ConnectionString = "Data Source=ISNTSAGE;Initial Catalog=pubs;Integrated Security=SSPI"; string query = "SELECT * FROM stores"; this.sqlDataAdapter1.SelectCommand.CommandText = query; this.sqlDataAdapter1.SelectCommand.Connection = sqlConnection1; this.sqlConnection1.Open(); this.sqlDataAdapter1.Fill(this.dataSet1, "storesAll"); this.DataGrid1.DataSource = this.dataSet1.Tables["storesAll"]; this.DataGrid1.DataBind(); this.sqlConnection1.Close(); } } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { this.DataGrid1.EditItemIndex = e.Item.ItemIndex; } </PRE>
Hi,Diane, you need to rebind your DataGrid in the EditCommand event after assigning the EditItemIndex. [quoted text, click to view] "Diane" wrote: > Oh misery. I have tricked myself. It's still broken. I forgot to > only load the data on postback = = false. It's still broken. > > Woe is me, the world is gray and all joy is gone from living. > > It's so tantalizing, because the page almost works well, excpet for the > pesky double-click. Sigh > > My Code: > > <PRE> > private void Page_Load(object sender, System.EventArgs e) > { > if (this.IsPostBack == false) > { > sqlConnection1.ConnectionString = "Data Source=ISNTSAGE;Initial > Catalog=pubs;Integrated Security=SSPI"; > string query = "SELECT * FROM stores"; > this.sqlDataAdapter1.SelectCommand.CommandText = query; > this.sqlDataAdapter1.SelectCommand.Connection = sqlConnection1; > > this.sqlConnection1.Open(); > this.sqlDataAdapter1.Fill(this.dataSet1, "storesAll"); > this.DataGrid1.DataSource = this.dataSet1.Tables["storesAll"]; > this.DataGrid1.DataBind(); > > this.sqlConnection1.Close(); > } > } > > private void DataGrid1_EditCommand(object source, > System.Web.UI.WebControls.DataGridCommandEventArgs e) > { > this.DataGrid1.EditItemIndex = e.Item.ItemIndex; > } > </PRE> >
Don't see what you're looking for? Try a search.
|