Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Friday, September 14, 2007

Modifying the contents of a GridView after DataBinding

This is more than bare functionality, but it can be simplified by eliminating the contents in foreach brackets.


protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow dgrow in GridView1.Rows)
{
String myString, myPattern;
Boolean result;
myPattern = txtSearch.Text;
myString = dgrow.Cells[0].Text;
result = true;

if (isEndsWith == true)
{
if (myPattern.Length > 0)
{
result = myString.EndsWith(myPattern);
dgrow.Cells[0].Text = myString.Substring(0, myString.LastIndexOf(myPattern))
+ HighlightText(myPattern, myString.Substring(myString.LastIndexOf(myPattern)));
}
else result = false;
if (result == false)
dgrow.Cells[0].Visible = false;
}
else
{
if (isStartsWith)
{
if (myPattern.Length > 0)
{
result = myString.StartsWith(myPattern);
dgrow.Cells[0].Text = HighlightText(myPattern, myString.Substring(0,myPattern.Length)) + myString.Substring(myPattern.Length, myString.Length-myPattern.Length);
dgrow.Cells[0].Visible = true;
}
}
else
{
dgrow.Cells[0].Text = HighlightText(myPattern, myString);
dgrow.Cells[0].Visible = true;
}
}
}
}

No comments: