Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i implemented vertical grid view.here every time bind one column values.the grid is like below.


header1|value1
Header2|Value2
Header3|Value3

here i need to read the values columns like value1,value2,value3.After that if the value is Zero i need to put one icon,If the value is -ve i need to put the different icon,like that if value is +ve

i am using the below code.

in .aspx i am using below code

XML
<asp:GridView ID="gridScore" runat="server" OnRowDataBound="gridScore_RowDataBound" OnRowCreated="gridScore_RowCreated" CssClass="myGrid" Font-Size="Small">
</asp:GridView>


in aspx.cs i am using the below code
C#
protected void BindGridviewData()
        {
            string DataId = Request.QueryString["q"];
            DataTable Score_Grid = new DataTable();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("PROC_DO_Sector_Influencer", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@DO_Ticker", SqlDbType.VarChar, 50).Value = DataId;
            //SqlDataReader reader = cmd.ExecuteReader();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(Score_Grid);
            gridScore.DataSource = Score_Grid;
            //gridScore.DataBind();
            gridScore.DataSource = ConvertColumnsAsRows(Score_Grid);
            gridScore.DataBind();
            gridScore.HeaderRow.Visible = false;
        }


 protected void gridScore_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].CssClass = "gridcss";
            }
        }

        public DataTable ConvertColumnsAsRows(DataTable dt)
        {
            DataTable dtnew = new DataTable();
            //Convert all the rows to columns
            for (int i = 0; i <= dt.Rows.Count; i++)
            {
                dtnew.Columns.Add(Convert.ToString(i));
            }
            DataRow dr;
            // Convert All the Columns to Rows
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                dr = dtnew.NewRow();
                dr[0] = dt.Columns[j].ToString();
                for (int k = 1; k <= dt.Rows.Count; k++)
                    dr[k] = dt.Rows[k - 1][j];
                dtnew.Rows.Add(dr);
            }
            return dtnew;
        }

     protected void gridScore_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow HeaderRow = new GridViewRow(1, 0, DataControlRowType.Header,DataControlRowState.Insert);
 
            TableCell HeaderCell2 = new TableCell();
            HeaderCell2.Text = "Score Influncers";
            HeaderCell2.ColumnSpan = 2;
            HeaderCell2.BackColor = Color.FromArgb(0xA7A6AA);
           // HeaderCell2.Font.Size="12"
                //HeaderCell2.Font = new Font("Serif", 24,FontStyle.Bold);
            HeaderCell2.Font.Size = 12;
            HeaderRow.Cells.Add(HeaderCell2);
            gridScore.Controls[0].Controls.AddAt(0, HeaderRow); 

        }
    }

any one can you please share idea or code to me.
Posted
Updated 10-Jun-15 2:21am
v6
Comments
Andy Lanng 9-Jun-15 4:22am    
Does the grid have a datasource?
if the grid is input only then please post the code you used to construct it.

Thanks
krish2013 9-Jun-15 9:05am    
Hi Andy,
Thanks for responding to my post,I shared my code.can you please look at the code and reply to me.
njammy 9-Jun-15 5:16am    
Are the headers going to be static list of items? Or may they change?

1 solution

Ok - I see.

Please remember to use the code formatting options in your posts (that's the yellow 'code' button at the top of the textarea. It adds code tags to your post. I have updated your question for you, this time.

The way that I would do this is:
1: <deleted> actually didn't need this
2: add another event handler to the gridview for DataBound which will occur after the data is bound and the other rows have been added.
3: In the event handler, sum the columns (pseudo code below) and create a gridview footer row
4: add the footer to the gridview rows.

C#
//I took the liberty of formatting your code as well


protected void BindGridviewData()
{
    string dataId = Request.QueryString["q"];
    DataTable scoreGrid = new DataTable();

    //This will clean up connections
    using (SqlConnection conn = new SqlConnection(connString))
    {   //this will help cleanup connections
        using (SqlCommand cmd = new SqlCommand("PROC_DO_Sector_Influencer", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@DO_Ticker", SqlDbType.VarChar, 50).Value = dataId;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            //Open the connection only when you need it
            conn.Open();
            da.Fill(scoreGrid );
            // ALWAYS close it
            conn.Close();
        }
    }
    gridScore.DataSource = ConvertColumnsAsRows(scoreGrid );
    gridScore.DataBind();
    gridScore.HeaderRow.Visible = false;
}

//This could prolly be better.  i haven't tested it either
protected void gridScore_DataBound(object sender, EventArgs e)
{
    // Grab the source from the grid.  It will not have the extra heading column
    DataTable dt = gridScore.DataSource as DataTable;
    if(dt==null)
        return;

    int[] footerValues = new int[dt.Columns.Count];

    //Loop through each row...
    foreach (DataRow row in dt.Rows)
    {
        //... and column
        for(int x = 0;x<dt.columns.count;x++)>
        {
            footerValues[x] += int.Parse(row[x].ToString());
        }
    }

    GridViewRow footerRow = gridScore.FooterRow;
    footerRow.Cells.Add(new TableHeaderCell
    {
        Text = @"Totals",
        ColumnSpan = 2,
        BackColor = Color.FromArgb(0xA7A6AA)
    });

    foreach (int footerValue in footerValues)
    {
        // get your image based in the value
        System.Web.UI.WebControls.Image image = GetIconFromValue(footerValue);

        TableCell cell = new TableCell();
        cell.Controls.Add(image);
        // you may need to iterate through the footer cells instead
        footerRow.Cells.Add(cell);
    }


}

protected void gridScore_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].CssClass = "gridcss";
    }
}

public DataTable ConvertColumnsAsRows(DataTable dt)
{
    DataTable dtnew = new DataTable();
    //Convert all the rows to columns
    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        dtnew.Columns.Add(Convert.ToString(i));
    }
    // Convert All the Columns to Rows
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        var dr = dtnew.NewRow();
        dr[0] = dt.Columns[j].ToString();
        for (int k = 1; k <= dt.Rows.Count; k++)
            dr[k] = dt.Rows[k - 1][j];
        dtnew.Rows.Add(dr);
    }
    return dtnew;
}

protected void gridScore_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridViewRow headerRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

        TableCell headerCell = new TableCell
        {
            Text = @"Score Influncers",
            ColumnSpan = 2,
            BackColor = Color.FromArgb(0xA7A6AA)
        };

        headerCell.Font.Size = 12;
        headerRow.Cells.Add(headerCell);
        gridScore.Controls[0].Controls.AddAt(0, headerRow);

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900