如何使用c#读取垂直网格查看列值 [英] how to read vertical grid View columns values using c#
问题描述
大家好,
i实现了垂直网格视图。每次都绑定一个列值。网格如下所示。
header1 | value1
Header2 | Value2
Header3 | Value3
这里我需要读取value1,value2,value3之类的值。之后如果值为Zero我需要放一个图标,如果值是-ve我需要放不同的图标,如果值为+ ve
i我正在使用以下代码。
in .aspx我正在使用以下代码
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
<asp:GridView ID="gridScore" runat="server" OnRowDataBound="gridScore_RowDataBound" OnRowCreated="gridScore_RowCreated" CssClass="myGrid" Font-Size="Small">
</asp:GridView>
aspx.cs中的
我正在使用以下代码
in aspx.cs i am using the below code
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.
推荐答案
好的 - 我明白了。
请记住在帖子中使用代码格式化选项(这是黄色的'代码'按钮textarea的顶部。它为你的帖子添加了代码标签。这次我已经为你更新了你的问题。
我这样做的方式是:
1:< deleted>实际上不需要这个
2:为gridview添加另一个事件处理程序DataBound
绑定数据并添加其他行后将发生的情况。
3:在事件处理程序中,对列进行求和(下面的伪代码)并创建gridview页脚行
4:将页脚添加到gridview行。
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 forDataBound
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.
//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);
}
}
这篇关于如何使用c#读取垂直网格查看列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!