Monday, March 12, 2012

Removing rows from a SqlDataSource before it is bound to a GridView?

I have a SqlDataSource that I need to remove the first 3 rows from before it is bound to a GridView. How would I go about doing this?

(if I could remove them at the db level in the sproc I would, but right now that is not an option - so I need to do it once I've already received the data)

Thanks.

HI~ There's no such flexibility for SqlDataSource yet. I'm afraid you should databind in code to perform this~ Like return a DataTable and remove the first 2 rows and then bind it to some control|||HI~ There's no such flexibility for SqlDataSource yet. I'm afraid you should databind in code to perform this~ Like return a DataTable and remove the first 3 rows and then bind it to some control|||

If I can avoid rebuilding all of these at this point, I'm going to try...

Is there a way for me to just filter out the first n records of a GridView every time it is loaded?

|||

Well , try the following

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 0)
{
e.Row.Visible = false;
}
}

When an element is Visible = false, it's not rendered at all~

|||

Beauty, thanks. I've been staring at this so long I didn't even think of that route.

One other question for you while I've got someone staring at this thread...if you don't mind.

Part of these same GridViews, the SqlDataSource UPDATE requires that one of the UpdateParameters be passed in as an XML string. Now, I have an XML UpdateParameter setup, it's just a question of how do I get my GridView editable fields for a given row, formatted in to the XML string I need to pass and then bound to the UpdateParameter?

I guess this is a two part question:

1) What event handler could I trap before the UPDATE, to cycle through the GridView's fields, format them in code into the XML string, and then...

2) How do I bind this new string to the UpdateParameter itself? Store it in session and bind the UpdateParameter to that? Not the most ideal...but just throwing ideas out there.

Thanks in advance, really appreciate it.

|||

Hi~ You have one filed in GrdiView row, and you wanna make some change to the value of this field before updating right?

I'm not quite sure, but you can try:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.NewValues["XXX"] = XmlFormat(e.NewValues["XXX"]);
}

|||

Hi there.

Not quite...one of the UpdateParameters for this particular GridView actually takes the values from 3 other fields in an edited row, combines them into an XML string, and then that value (the XML string that has values from 3 of the fields in it), is bound to the UpdateParameter.

Does that make better sense? :)

|||

That 3 fields should be in e.OldValues or e.NewValues collection, You may try both, depending on how you databind them (Eval() or Bind()), or neither.

For example:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.NewValues["XML"] = XmlFormat(e.OldValues["field1"], e.OldValues["field2"], e.OldValues["field3"]);
}

The above is not rwWord("guarantee"); guaranteed to work, usually I would use ObjectDataSource and my own BLL method / Or code behind to do this kind of complex operation... SqlDataSource is not good at this..

|||

Yup, I couldn't agree with you more on the ObjectDataSource/BLL route...unfortunately we have a *very* limited budget on this project, and we're inheriting some things from the previous vendor. With the timeframe and budget I'm trying to work with what's already there in the interest of just getting it done for the first go. Once we have more time available to us, we'll be able to do it right with the business layer, until then...workarounds and hacks. =/

Thanks for your help on this tonight, I'll let you know how it turns out.

Best.

No comments:

Post a Comment