Free Support Forum - aspose.com

Displaying one row in different color

hello

iam used this code for displaying one row in different color

int i = 1;

while (i <= dt.Rows.Count)

{

string d = sheet1.Cells["K" + i + ""].Value.ToString();

if (d == DrawingId)

{

int rowid = sheet1.Cells["K" + i + ""].Row;

//for (int g = 0; g <= sheet1.Cells.MaxColumn; g++)

//{

sheet1.Cells[i - 1, 0].Style.BackColor = System.Drawing.Color.Blue;

//sheet1.Cells[1, 0].Style.BackColor = System.Drawing.Color.Blue;

//}

}

i++;

}

}

but its displaying alternate rows in blue color.

can you tell me the solution for this.

i want to display only one row in different color

Hi,

I am not very sure about your need. But it seems that you want to search a value into a column in the worksheet of the gridweb and then set the shading color to that row involving the cell value (found).

Suppose if you to search a value i.e. "RATTC" in the second colomn B in the first worksheet of the gridweb. If the value is found then the whole row's background color involving the value will be red.

Here is my code which works fine:

Session["drawingid"]= "RATTC";
.

.

.

.

string val = (string)Session["drawingid"];
WebCells cells = GridWeb1.WebWorksheets[0].Cells;
WebCell cell;
int rowid=0;
//Scan the second column to match the value.
for (int i=0;i<=GridWeb1.WebWorksheets[0].Cells.MaxRow;i++)
{
cell = cells[i,1];
if(cell.StringValue == val)
{
rowid = cell.Row;
for (int y=0;y<=GridWeb1.WebWorksheets[0].Cells.MaxColumn;y++)
{
GridWeb1.WebWorksheets[0].Cells[rowid,y].Style.BackColor = Color.Red;
}
break;
}

}


I think you may change your code accordingly for your need.

Thank you.

hello,

i implemented this code only. but its displaying alternate rows in red color.

Hi,

Could you create a small test asp.net project using the code to display how it is displaying altervative rows with their back color red, zip the project (with .aspx, .cs/vb files) and post us here. So, that we may check and figure it our soon.

Thank you.

hello

when we give pre defined values in the grid by using putvalue method its working fine.

but iam using dataset here. iam binding dataset to grid. its giving the problem here.

protected void loadgrid()

{

GridWeb1.WebWorksheets[0].Cells[0, 0].PutValue("123");

GridWeb1.WebWorksheets[0].Cells[0, 1].PutValue("gdfgd dfgdf");

GridWeb1.WebWorksheets[0].Cells[1, 0].PutValue("124");

GridWeb1.WebWorksheets[0].Cells[1, 1].PutValue("fsfds");

GridWeb1.WebWorksheets[0].Cells[2, 0].PutValue("125");

GridWeb1.WebWorksheets[0].Cells[2, 1].PutValue("dsfsd fsdf");

string val = "123";

WebCells cells = GridWeb1.WebWorksheets[0].Cells;

WebCell cell;

int rowid = 0;

//Scan the second column to match the value.

for (int i = 0; i <= GridWeb1.WebWorksheets[0].Cells.MaxRow; i++)

{

cell = cells[i, 0];

if (cell.StringValue == val)

{

rowid = cell.Row;

for (int y = 0; y <= GridWeb1.WebWorksheets[0].Cells.MaxColumn; y++)

{

GridWeb1.WebWorksheets[0].Cells[rowid, y].Style.BackColor = System.Drawing.Color.Red;

}

break;

}

}

}

if i do the code like this there is no problem.

but my code is

protected void loadGridWeb()

{

SqlDataAdapter daClarifications = new SqlDataAdapter("SELECT * FROM TblDrawing where ProjectId='" + ProjectId + "'", con);

DataSet dsClarifications = new DataSet();

daClarifications.Fill(dsClarifications, "TblNote");

WebWorksheet sheet1 = this.GridWeb1.WebWorksheets[0];

sheet1.Cells.Clear();

DataTable dt = dsClarifications.Tables[0];

if (dt.Rows.Count > 0)

{

ProjectId = dt.Rows[0].ItemArray[0].ToString();

}

sheet1.DataSource = dsClarifications;

//sheet1.CreateAutoGenratedColumns();

sheet1.DataBind();

string val = DrawingId;

WebCells cells = GridWeb1.WebWorksheets[0].Cells;

WebCell cell;

int rowid = 0;

//Scan the second column to match the value.

for (int i = 0; i <= GridWeb1.WebWorksheets[0].Cells.MaxRow; i++)

{

cell = cells[i, 10];

if (cell.StringValue == val)

{

rowid = cell.Row;

for (int y = 0; y <= GridWeb1.WebWorksheets[0].Cells.MaxColumn; y++)

{

GridWeb1.WebWorksheets[0].Cells[rowid, y].Style.BackColor = System.Drawing.Color.Red;

}

break;

}

}

}

when i use this code its displaying alternate rows in red color.

Hi,

Thanks for the code and explanation.

We will check and get back to you to provide with the solution soon.

Thanks for being patient!

swapna:

hello

when we give pre defined values in the grid by using putvalue method its working fine.

but iam using dataset here. iam binding dataset to grid. its giving the problem here.

protected void loadgrid()

{

GridWeb1.WebWorksheets[0].Cells[0, 0].PutValue("123");

GridWeb1.WebWorksheets[0].Cells[0, 1].PutValue("gdfgd dfgdf");

GridWeb1.WebWorksheets[0].Cells[1, 0].PutValue("124");

GridWeb1.WebWorksheets[0].Cells[1, 1].PutValue("fsfds");

GridWeb1.WebWorksheets[0].Cells[2, 0].PutValue("125");

GridWeb1.WebWorksheets[0].Cells[2, 1].PutValue("dsfsd fsdf");

string val = "123";

WebCells cells = GridWeb1.WebWorksheets[0].Cells;

WebCell cell;

int rowid = 0;

//Scan the second column to match the value.

for (int i = 0; i <= GridWeb1.WebWorksheets[0].Cells.MaxRow; i++)

{

cell = cells[i, 0];

if (cell.StringValue == val)

{

rowid = cell.Row;

for (int y = 0; y <= GridWeb1.WebWorksheets[0].Cells.MaxColumn; y++)

{

GridWeb1.WebWorksheets[0].Cells[rowid, y].Style.BackColor = System.Drawing.Color.Red;

}

break;

}

}

}

if i do the code like this there is no problem.

but my code is

protected void loadGridWeb()

{

SqlDataAdapter daClarifications = new SqlDataAdapter("SELECT * FROM TblDrawing where ProjectId='" + ProjectId + "'", con);

DataSet dsClarifications = new DataSet();

daClarifications.Fill(dsClarifications, "TblNote");

WebWorksheet sheet1 = this.GridWeb1.WebWorksheets[0];

sheet1.Cells.Clear();

DataTable dt = dsClarifications.Tables[0];

if (dt.Rows.Count > 0)

{

ProjectId = dt.Rows[0].ItemArray[0].ToString();

}

sheet1.DataSource = dsClarifications;

//sheet1.CreateAutoGenratedColumns();

sheet1.DataBind();

string val = DrawingId;

WebCells cells = GridWeb1.WebWorksheets[0].Cells;

WebCell cell;

int rowid = 0;

//Scan the second column to match the value.

for (int i = 0; i <= GridWeb1.WebWorksheets[0].Cells.MaxRow; i++)

{

cell = cells[i, 10];

if (cell.StringValue == val)

{

rowid = cell.Row;

for (int y = 0; y <= GridWeb1.WebWorksheets[0].Cells.MaxColumn; y++)

{

GridWeb1.WebWorksheets[0].Cells[rowid, y].Style.BackColor = System.Drawing.Color.Red;

}

break;

}

}

}

when i use this code its displaying alternate rows in red color.

Hi,

I think this line could have some problems:

if (cell.StringValue == val)

After DataBind, the cell value's data type could be "Double", "Integer", etc, Which is the same as the dataset. And the cell could contain NumberType setting. So the StringValue could be a formatted string which doesn't equal to the "val".

And I don't know the returned fields and their data type of your sql because that you used a "*" in the sql. You may give us more details.

Thank you.

hello sir,.

if (cell.StringValue == val)

{

rowid = cell.Row;

for (int y = 0; y <= GridWeb1.WebWorksheets[0].Cells.MaxColumn; y++)

{

GridWeb1.WebWorksheets[0].Cells[rowid, y].Style.BackColor = System.Drawing.Color.Red;

}

break;

}

this code executing properly. when i use break point its comparing values properly and executing only once when its find the matching values only. but after completing execution its displaying alternate rows in red color.

that's iam not understanding. its executing only once but its displaying alternate rows in red color.

swapna

Hi Swapna,

Well, Since you use data binding in your project, I think you may try an alternative client - side solution for your need.

Here is the sample code which works fine for data bound sheet. You may place a custom command button labeled "Search Rec" using Custom Command Button Editor of the GridWeb.

In Page_Load event handler you may write the following lines of code after binding worksheet in the GridWeb.

if (!IsPostBack)
{
.
.
.
// Binding Worksheet.
sheet1.DataBind();
GridWeb1.Attributes["DrawingId"] = "Tofu";
GridWeb1.Attributes["maxrow"] = GridWeb1.WebWorksheets[0].Cells.MaxRow.ToString() ;
GridWeb1.Attributes["maxcol"] = GridWeb1.WebWorksheets[0].Cells.MaxColumn.ToString() ;
}

Client side code using javascript in the .... tags

.
.
.


When you click the gridweb's custom button, the search operation will be implemented and the related rec/row will be red with blue text.
Thank you.

Iam not understanding what to do

Hi,

Thanks for considering Aspose.

Could you elaborate which part of code you could not understand or what are the problems you are facing to implement the functionality. So that we may try to guide you through.

Thank you.

Hi swapna,

Thanks for considering Aspose.

I have created a sample Asp.Net solution to make you better understand and to implement your scenario. Attached is complete Asp.Net web solution which demonstrates how to search the specific value in a column in a bound worksheet and highlight the complete row / record in red color. I have created the solution using javascript client side code which will not highlight the alternative rows as you are facing the problem on the server-side code. Hopefully the project will give you some insight for your need.

Thank you.