Validation for datagrid c# code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey everyone,
I am trying to run some validation on the on-click event of a button to
check and see whether a datagrid contains any rows inserted into it and if it
does then I'd like it to work as normal. However if it has no items in it
then i'd like it to display the error message below and not move forward.
Here is the code I have but it doesn't seem to work. I think i've got the
wrong logic. Can someone help me work this out.
Thanks

private void cicmdPerformSearch_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (this.dgSearchAddresses.Items.Count == 0)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
Context.Items["TransferAddresses"] = addresses;
Server.Transfer("../Internet/WebForm2.aspx");
}
else
{
pnlMessages.Visible = true;
this.lblMessage.Text = "You must select at least one address";
}
}
Cheers again for any help anyone can give.
 
Hi,

Please remember that teh grid is only a way to show the content of a
collection of some kind in the page, with this I mean that it does not
contains data by itself.
Are you trying to see if the binded collection has any item at all?
Do you keep a reference to the data source used to bind the grid? if so that
is what you need to query about insertions, etc

Cheers,
 
Are you trying to see if the binded collection has any item at all?
Yes
The datagrid is binded to the addresses arraylist which is saved in the
viewstate object.

I know it doesn't contain data unless it is bound to something, but im
trying to figure out if it contains any rows and if it doesn't I don't want
the user to be able to move forward to a different page as the user must have
a least one row or the search is going to crash.

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Please remember that teh grid is only a way to show the content of a
collection of some kind in the page, with this I mean that it does not
contains data by itself.
Are you trying to see if the binded collection has any item at all?
Do you keep a reference to the data source used to bind the grid? if so that
is what you need to query about insertions, etc

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Stephen said:
Hey everyone,
I am trying to run some validation on the on-click event of a button to
check and see whether a datagrid contains any rows inserted into it and if it
does then I'd like it to work as normal. However if it has no items in it
then i'd like it to display the error message below and not move forward.
Here is the code I have but it doesn't seem to work. I think i've got the
wrong logic. Can someone help me work this out.
Thanks

private void cicmdPerformSearch_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (this.dgSearchAddresses.Items.Count == 0)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
Context.Items["TransferAddresses"] = addresses;
Server.Transfer("../Internet/WebForm2.aspx");
}
else
{
pnlMessages.Visible = true;
this.lblMessage.Text = "You must select at least one address";
}
}
Cheers again for any help anyone can give.
 
Back
Top