Home All Groups Group Topic Archive Search About
Author
26 Apr 2005 5:28 PM
postings
Hi

I've got a nasty problem with my datagrid. Sorry for the long post,
but I am trying to be clear and compact here.

FYI - About my datagrid:

-------------------------------------------------------------------

I have in the datagrid and an EditCommandButton like so:
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>

-------------------------------------------------------------------

There's also some drop down list boxes in the EditItemTemplate which
I am not populating with values until I hit the edit linkbutton. Note
that there are no controls are created dynamically in the datagrid.

The datagrid is set up and populated like so (triggered when somebody
hits a button to display it):

-------------------------------------------------------------------

Private Sub populateDGProductSummary()
SqlSelectProducts.CommandText = strQuery
Mydataset.ProductInvoiceSummary.Clear()
Mydataset.EnforceConstraints = False
MyDataAdapter.Fill(Mydataset.ProductInvoiceSummary)
MyDataGrid.DataSource = Mydataset.ProductInvoiceSummary
MyDataGrid.CurrentPageIndex = Session("DGPageProductSummary")
MyDataGrid.DataBind()
End Sub

-------------------------------------------------------------------

This works fine for the initial display (Note I am handling the opening
and closing of SQL just fine, I've just removed it from my code for
now).

SO FAR SO GOOD....

Now in the context of hitting the edit button and calling procudure and
repopulating datagrid using populateDGProductSummary again:

Everything works in my code (which you haven't seen yet). My drop down
listboxes work fine etc.

*** NEW SCENARIO THEN *** (hope you are still following)

What if I use the viewstate as a datasource rather than repopulate the
dataset bound to the datagrid (it's not incredibly efficient but it
will have to do )...

I have put in on the last line of populateDGProductSummary: subroutine:

-------------------------------------------------------------------

viewstate.Add("Datagriddata", DsContactStructure1)

-------------------------------------------------------------------

And here is the code for my Edit linkbutton:

-------------------------------------------------------------------

Private Sub DGProductSummary_Edit(ByVal source As System.Object, ByVal
e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
MyDataGrid.EditCommand

Response.Write("Triggered")
Mydataset.ProductInvoiceSummary.Clear()
Mydataset.EnforceConstraints = False
Dim dssaved As DataSet = CType(viewstate.Item("Datagriddata"), DataSet)
DAShowInvoiceProducts.Fill(dssaved)
MyDataGrid.DataSource = dssaved.Tables("ProductInvoiceSummary")

-------------------------------------------------------------------

So far so good (I can see the data if I iterate through the dataset
right now)....

*** THE PROBLEM **

The problem is when I populate the drop down lists in the datagrid
after this code.  If I have just repopulated the datagrid with using
the original populateDGProductSummary() then there would be no
problems. It's when I get the viewstate for the datagrid instead.

My code then to populate the listbox:

-------------------------------------------------------------------

Dim ddl As DropDownList
ddl = MyDataGrid.Items(e.Item.ItemIndex).FindControl("ddlCurrency")
ddl.Items.Add(New ListItem("HELLO", "WORLD"))

-------------------------------------------------------------------

Basically it seems that I cant find the control ddlCurrency any more
using Viewstate, I get the error message on the findcontrol line:

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index

Can anybody tell me what on earth is going on? Why can't I find the
control in this scenario?

Am I being thick?

Many thanks!!

Alex

(p.s. thanks for baring with me, if you read this far and still
understand what I'm going on about!)

Author
26 Apr 2005 8:52 PM
Elton W
Hi Alex,

Could you show whole HTML code of the datagrid?

Elton Wang
elton_w***@hotmail.com

Show quoteHide quote
>-----Original Message-----
>Hi
>
>I've got a nasty problem with my datagrid. Sorry for the
long post,
>but I am trying to be clear and compact here.
>
>FYI - About my datagrid:
>
>----------------------------------------------------------
---------
>
>I have in the datagrid and an EditCommandButton like so:
><asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update"
>CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
Show quoteHide quote
>
>----------------------------------------------------------
---------
>
>There's also some drop down list boxes in the
EditItemTemplate which
>I am not populating with values until I hit the edit
linkbutton. Note
>that there are no controls are created dynamically in the
datagrid.
>
>The datagrid is set up and populated like so (triggered
when somebody
>hits a button to display it):
>
>----------------------------------------------------------
---------
>
>Private Sub populateDGProductSummary()
>SqlSelectProducts.CommandText = strQuery
>Mydataset.ProductInvoiceSummary.Clear()
>Mydataset.EnforceConstraints = False
>MyDataAdapter.Fill(Mydataset.ProductInvoiceSummary)
>MyDataGrid.DataSource = Mydataset.ProductInvoiceSummary
>MyDataGrid.CurrentPageIndex = Session
("DGPageProductSummary")
>MyDataGrid.DataBind()
>End Sub
>
>----------------------------------------------------------
---------
>
>This works fine for the initial display (Note I am
handling the opening
>and closing of SQL just fine, I've just removed it from
my code for
>now).
>
>SO FAR SO GOOD....
>
>Now in the context of hitting the edit button and calling
procudure and
>repopulating datagrid using populateDGProductSummary
again:
>
>Everything works in my code (which you haven't seen yet).
My drop down
>listboxes work fine etc.
>
>*** NEW SCENARIO THEN *** (hope you are still following)
>
>What if I use the viewstate as a datasource rather than
repopulate the
>dataset bound to the datagrid (it's not incredibly
efficient but it
Show quoteHide quote
>will have to do )...
>
>I have put in on the last line of
populateDGProductSummary: subroutine:
>
>----------------------------------------------------------
---------
>
>viewstate.Add("Datagriddata", DsContactStructure1)
>
>----------------------------------------------------------
---------
>
>And here is the code for my Edit linkbutton:
>
>----------------------------------------------------------
---------
>
>Private Sub DGProductSummary_Edit(ByVal source As
System.Object, ByVal
Show quoteHide quote
>e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
>MyDataGrid.EditCommand
>
>Response.Write("Triggered")
>Mydataset.ProductInvoiceSummary.Clear()
>Mydataset.EnforceConstraints = False
>Dim dssaved As DataSet = CType(viewstate.Item
("Datagriddata"), DataSet)
>DAShowInvoiceProducts.Fill(dssaved)
>MyDataGrid.DataSource = dssaved.Tables
("ProductInvoiceSummary")
>
>----------------------------------------------------------
---------
>
>So far so good (I can see the data if I iterate through
the dataset
>right now)....
>
>*** THE PROBLEM **
>
>The problem is when I populate the drop down lists in the
datagrid
>after this code.  If I have just repopulated the datagrid
with using
>the original populateDGProductSummary() then there would
be no
>problems. It's when I get the viewstate for the datagrid
instead.
>
>My code then to populate the listbox:
>
>----------------------------------------------------------
---------
>
>Dim ddl As DropDownList
>ddl = MyDataGrid.Items(e.Item.ItemIndex).FindControl
("ddlCurrency")
>ddl.Items.Add(New ListItem("HELLO", "WORLD"))
>
>----------------------------------------------------------
---------
>
>Basically it seems that I cant find the control
ddlCurrency any more
>using Viewstate, I get the error message on the
findcontrol line:
>
>System.ArgumentOutOfRangeException: Index was out of
range. Must be
>non-negative and less than the size of the collection.
Parameter name:
>index
>
>Can anybody tell me what on earth is going on? Why can't
I find the
Show quoteHide quote
>control in this scenario?
>
>Am I being thick?
>
>Many thanks!!
>
>Alex
>
>(p.s. thanks for baring with me, if you read this far and
still
>understand what I'm going on about!)
>
>.
>
Author
27 Apr 2005 10:04 AM
postings
Sure thing... Thanks Elton

Here we go: then:

-----------------------------------------------------------------------------------------------------------------------------------------------------------
<asp:Panel id="pnlProductDetails" runat="server">
<asp:DataGrid id=MyDataGrid runat="server" Font-Size="XX-Small"
DataSource='<%# DataBinder.Eval(MyDataSet,
"Tables[ProductInvoiceSummary]") %>' AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="ProductInvoiceID"
SortExpression="ProductInvoiceID"
HeaderText="ProductInvoiceID"></asp:BoundColumn>
<asp:BoundColumn DataField="ProductContactID"
SortExpression="ProductContactID"
HeaderText="ProductContactID"></asp:BoundColumn>
<asp:BoundColumn DataField="InvoiceID" SortExpression="InvoiceID"
HeaderText="InvoiceID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Quantity") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblQuantity runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Quantity") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblName runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Name") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CompanyName">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.CompanyName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblAgency runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.CompanyName") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="DateEntered">
<ItemTemplate>
<asp:Label id=Label4 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DateEntered", "{0:d}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblDateEntered2 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.DateEntered", "{0:d}") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="StartDate">
<ItemTemplate>
<asp:Label id=Label5 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.StartDate", "{0:d}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblStartDate runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.StartDate", "{0:d}") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="EndDate">
<ItemTemplate>
<asp:Label id=Label6 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.EndDate", "{0:d}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=lblEndDate runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.EndDate", "{0:d}") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CurrencyID">
<HeaderTemplate>
Currency
</HeaderTemplate>
<ItemTemplate>
<asp:Label id=lblCurrencyID runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.CurrencyID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCurrency" runat="server"
Font-Size="XX-Small"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Price">
<ItemTemplate>
<asp:Label id=Label8 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Price") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtPrice ontextchanged="txtPrice_TextChanged"
runat="server" Font-Size="XX-Small" Width="75px" Text='<%#
DataBinder.Eval(Container, "DataItem.Price") %>' AutoPostBack="True">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Discount">
<ItemTemplate>
<asp:Label id=Label9 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Discount") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtDiscount runat="server" Font-Size="XX-Small"
Width="75px" Text='<%# DataBinder.Eval(Container, "DataItem.Price") %>'
AutoPostBack="True">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="TaxID">
<ItemTemplate>
<asp:Label id=Label7 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.TaxID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlTaxes" runat="server"
Font-Size="XX-Small"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Total">
<ItemTemplate>
<asp:Label id="Label10" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtTotal runat="server" Font-Size="XX-Small"
Width="75px" Text='<%# DataBinder.Eval(Container, "DataItem.Price")
%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="RenewalStatus">
<ItemTemplate>
<asp:CheckBox id="chkRenewalLabel" runat="server"
Enabled="False"></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id="chkRenewal" runat="server"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Note">
<ItemTemplate>
<asp:Label id=Label11 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Note") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtNote runat="server" Font-Size="XX-Small" Text='<%#
DataBinder.Eval(Container, "DataItem.Note") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<P></P>
</asp:Panel>
-----------------------------------------------------------------------------------------------------------------------------------------------------------

Cheers!

Alex
Author
27 Apr 2005 1:30 PM
Elton W
Hi Alex,

When using in-line edit datagrid, any item of the datagrid
has two states, normal and editable. In normal state it
shows (has) staff in ItemTemplate. And in editable state,
it shows (has) staff in EditItemTemplate. In order to get
editable staff, such as ddl, checkbox, and so on, you need
shift item state to editable first then use FindControl
method:

MyDataGrid.EditItemIndex = e.Item.ItemIndex  
Dim dssaved As DataSet = CType(viewstate.Item
("Datagriddata"), DataSet)
' You already have dataset with data. Why do you fill it?
' DAShowInvoiceProducts.Fill(dssaved)
MyDataGrid.DataSource = dssaved.Tables
("ProductInvoiceSummary")
MyDataGrid.DataBind()
' Now you can loop up Editable staff
Dim ddl As DropDownList
ddl = MyDataGrid.Items(e.Item.ItemIndex).FindControl
("ddlCurrency")
ddl.Items.Add(New ListItem("HELLO", "WORLD"))


HTH

Elton



Show quoteHide quote
>-----Original Message-----
>Hi
>
>I've got a nasty problem with my datagrid. Sorry for the
long post,
>but I am trying to be clear and compact here.
>
>FYI - About my datagrid:
>
>----------------------------------------------------------
---------
>
>I have in the datagrid and an EditCommandButton like so:
><asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update"
>CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
Show quoteHide quote
>
>----------------------------------------------------------
---------
>
>There's also some drop down list boxes in the
EditItemTemplate which
>I am not populating with values until I hit the edit
linkbutton. Note
>that there are no controls are created dynamically in the
datagrid.
>
>The datagrid is set up and populated like so (triggered
when somebody
>hits a button to display it):
>
>----------------------------------------------------------
---------
>
>Private Sub populateDGProductSummary()
>SqlSelectProducts.CommandText = strQuery
>Mydataset.ProductInvoiceSummary.Clear()
>Mydataset.EnforceConstraints = False
>MyDataAdapter.Fill(Mydataset.ProductInvoiceSummary)
>MyDataGrid.DataSource = Mydataset.ProductInvoiceSummary
>MyDataGrid.CurrentPageIndex = Session
("DGPageProductSummary")
>MyDataGrid.DataBind()
>End Sub
>
>----------------------------------------------------------
---------
>
>This works fine for the initial display (Note I am
handling the opening
>and closing of SQL just fine, I've just removed it from
my code for
>now).
>
>SO FAR SO GOOD....
>
>Now in the context of hitting the edit button and calling
procudure and
>repopulating datagrid using populateDGProductSummary
again:
>
>Everything works in my code (which you haven't seen yet).
My drop down
>listboxes work fine etc.
>
>*** NEW SCENARIO THEN *** (hope you are still following)
>
>What if I use the viewstate as a datasource rather than
repopulate the
>dataset bound to the datagrid (it's not incredibly
efficient but it
Show quoteHide quote
>will have to do )...
>
>I have put in on the last line of
populateDGProductSummary: subroutine:
>
>----------------------------------------------------------
---------
>
>viewstate.Add("Datagriddata", DsContactStructure1)
>
>----------------------------------------------------------
---------
>
>And here is the code for my Edit linkbutton:
>
>----------------------------------------------------------
---------
>
>Private Sub DGProductSummary_Edit(ByVal source As
System.Object, ByVal
Show quoteHide quote
>e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
>MyDataGrid.EditCommand
>
>Response.Write("Triggered")
>Mydataset.ProductInvoiceSummary.Clear()
>Mydataset.EnforceConstraints = False
>Dim dssaved As DataSet = CType(viewstate.Item
("Datagriddata"), DataSet)
>DAShowInvoiceProducts.Fill(dssaved)
>MyDataGrid.DataSource = dssaved.Tables
("ProductInvoiceSummary")
>
>----------------------------------------------------------
---------
>
>So far so good (I can see the data if I iterate through
the dataset
>right now)....
>
>*** THE PROBLEM **
>
>The problem is when I populate the drop down lists in the
datagrid
>after this code.  If I have just repopulated the datagrid
with using
>the original populateDGProductSummary() then there would
be no
>problems. It's when I get the viewstate for the datagrid
instead.
>
>My code then to populate the listbox:
>
>----------------------------------------------------------
---------
>
>Dim ddl As DropDownList
>ddl = MyDataGrid.Items(e.Item.ItemIndex).FindControl
("ddlCurrency")
>ddl.Items.Add(New ListItem("HELLO", "WORLD"))
>
>----------------------------------------------------------
---------
>
>Basically it seems that I cant find the control
ddlCurrency any more
>using Viewstate, I get the error message on the
findcontrol line:
>
>System.ArgumentOutOfRangeException: Index was out of
range. Must be
>non-negative and less than the size of the collection.
Parameter name:
>index
>
>Can anybody tell me what on earth is going on? Why can't
I find the
Show quoteHide quote
>control in this scenario?
>
>Am I being thick?
>
>Many thanks!!
>
>Alex
>
>(p.s. thanks for baring with me, if you read this far and
still
>understand what I'm going on about!)
>
>.
>
Author
27 Apr 2005 3:16 PM
postings
Hi Elton!

You're absolutely right but I still have the same error message in the
same place.

Firstly I forgot to post: MyDataGrid.EditItemIndex = e.Item.ItemIndex
in the code.
(sorry, I simplified the code too much for presentation purposes!).

Secondly I shouldn't have had the data adapter fill command (don't know
why it was there).

So my code now looks like:

----------------------------------------------------------------------------------------

Private Sub DGProductSummary_Edit(ByVal source As System.Object, ByVal
e As System.Web.UI.WebControls.Data­GridCommandEventArgs) Handles
MyDataGrid.EditCommand

Response.Write("Triggered")

MyDataGrid.EditItemIndex = e.Item.ItemIndex
Dim dssaved As DataSet = CType(viewstate.Item("Datagriddata"), DataSet)
MyDataGrid.DataSource = dssaved.Tables("ProductInvoiceSummary")
MyDataGrid.DataBind

Dim ddl As DropDownList
ddl = MyDataGrid.Items(e.Item.ItemIn­dex).FindControl("ddlCurrency")
ddl.Items.Add(New ListItem("HELLO", "WORLD"))

etc...

----------------------------------------------------------------------------------------

Note this is what I'm doing when I'm initially populating the datagrid
(being displayed for the first time).
Is it the way the viewstate is being saved?

----------------------------------------------------------------------------------------

SqlSelectProducts.CommandText = strQuery
DsContactStructure1.ProductInvoiceSummary.Clear()
DsContactStructure1.EnforceConstraints = False
DAShowInvoiceProducts.Fill(DsContactStructure1.ProductInvoiceSummary)
MyDataGrid.DataSource = DsContactStructure1.ProductInvoiceSummary
MyDataGrid.databind
viewstate.Add("Datagriddata", DsContactStructure1)
'Note also tried binding the datagrid after adding the viewstate
instead

----------------------------------------------------------------------------------------

Any further ideas?

Cheers!

Alex
Author
27 Apr 2005 4:15 PM
postings
FYI

I've published my full code here (very much a work in progress mind, so
a lot of it's pretty ugly!).
http://www.alexshirley.com/mycode.txt

Hope this is helpful:

Before I was stripping out what I thought was important to avoid
confusion.
The last post I was pretty accurate I think so you may want to look at
this first.

Thanks

Alex
Author
28 Apr 2005 1:41 PM
postings
Found the solution!!!!
I was spending too much time looking at the back end code and not
looking at the front end (DOH!).

Changed:
<asp:DataGrid id=DGProductSummary runat="server" Font-Size="XX-Small"
AutoGenerateColumns="False" DataSource='<%#
DataBinder.Eval(DsContactStructure1, "Tables[ProductInvoiceSummary]")
%>'>

to

<asp:DataGrid id="DGProductSummary" runat="server" Font-Size="XX-Small"
AutoGenerateColumns="False">

I wasn't paying attention to my databinding on the front end code
(doh!)

Thanks for helping Elton (I was very close to giving up completely).

Alex