ASP .NET DataGrid Dynamic(OnrowDatabound)通过标签显示Diverentz通过Collor找到所有单元格

发布于 2025-02-01 17:54:50 字数 4436 浏览 6 评论 0原文

我有一个特殊的问题要解决,可以找到解决方案。

DataGrid绑定到SQL查询,并且具有可检测到历史更改的动态内部连接。用户可以选择任何导入的数据以显示多达10个数据库之间的差异。

列的标签是Epreis和bpreis。
现在,我需要在行上找到所有Epreis和所有bpreis鸡蛋5xepreis。

funktion1 bpreis最便宜的背景颜色=绿色 Secend Preis背景颜色= Yello 3 ...... 10.
在Epreis上也是如此。

C#

protected void run_Click(object sender, EventArgs e)
{
    SqlDataAdapter adp1 = new SqlDataAdapter(getSearch, con);
    DataTable dt1 = new DataTable();
    adp1.Fill(dt1);
    mergeresult.DataSource = dt1;
    mergeresult.DataBind();
}

protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)        
{
    if (e.Row.RowIndex >= 0)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.count - 1; i++)
            { 
                for (int cellIndex = 0; cellIndex < Row.Cells.Count; cellIndex++)
                {
                    if ((Label)e.row.cell.cellindex == "EPreis") 
                    {
                        
                            //? Howto find all PReis and EPreis in row.count
                            //  is posible to put cell[x] and cell.text in array
                            // and at end of row sorting array 1 to x to colering
                            // the cell.

                        // Later to expand funktion Tolltip or mous over to show %
                        //   Diverent to cheeperst
                    }
                }
            }
        }
    }
} 

我找到了一个解决方案,但它仍然无法正常工作。 最高和最低的价格有时会逆转。

在此处输入图像描述

  if (e.Row.RowIndex >= 0)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int totalRow = mergeresult.Rows.Count;
                int totalColumns = mergeresult.Columns.Count;

                for (int i = 0; i < totalRow + 1; i++)
                {
                    //int anzahl = (int)ViewState["Anzahlfrom"];
                    //string[] NumOfPreisfields = new string[anzahl];
                    var Preisfields = new List<string>();
                    var Preisfieldslist = new List<(int cellNr, string Preis, string color)>();

                    for (int j = 0; j < e.Row.Cells.Count; j++)
                    {
                        var SearchColumnName = "EK";
                        DataControlFieldCell cell = (DataControlFieldCell)e.Row.Cells[j];

                        if (cell.ContainingField is BoundField)
                        {
                            if (((BoundField)cell.ContainingField).DataField.Contains(SearchColumnName))
                            {
                                Preisfields.Add(j.ToString());
                                Preisfieldslist.Add((j, cell.Text.ToString(), ""));
                                //cell.ForeColor = System.Drawing.Color.Red;
                            }
                        }

                        if (j == e.Row.Cells.Count -1)
                        {
                            //var SortedPreisfieldList = Preisfieldslist.OrderBy(r => r.Preis);
                            var LowerstPreis = Preisfieldslist.Min(r => r.Preis);
                             var HiegerstPreis = Preisfieldslist.Max(r => r.Preis);

                                 for (int c = 0; c < Preisfieldslist.Count; c++)
                                 {
                                     if (LowerstPreis == Preisfieldslist[c].Preis)
                                        {
                                        int n = Preisfieldslist[c].cellNr;
                                        e.Row.Cells[n].ForeColor = System.Drawing.Color.Green;
                                        }
                                        
                                        if (HiegerstPreis == Preisfieldslist[c].Preis)
                                        {
                                        int n = Preisfieldslist[c].cellNr;
                                        e.Row.Cells[n].ForeColor = System.Drawing.Color.Red;
                                 }
                            }
                        }
                    }
                }
            } // ende for (int i = 0; i < totalRow +1 ; i++)

        }

I have a special thing to solve and can find a solution.

The Datagrid is bound to SQL query and has dynamic inner Join that is detectable all-time changing. The user can select any imported Data to show differences between up to 10 databases.

The Labelname of columns is EPreis and BPreis.
Now I need to find all EPreis and all BPreis eggs 5xBPreis 5xEPreis on Row.

Funktion1
BPreis cheapest Background color = green
Secend Preis Background Color = Yello
3...... 10.
The same on EPreis.

C#

protected void run_Click(object sender, EventArgs e)
{
    SqlDataAdapter adp1 = new SqlDataAdapter(getSearch, con);
    DataTable dt1 = new DataTable();
    adp1.Fill(dt1);
    mergeresult.DataSource = dt1;
    mergeresult.DataBind();
}

protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)        
{
    if (e.Row.RowIndex >= 0)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.count - 1; i++)
            { 
                for (int cellIndex = 0; cellIndex < Row.Cells.Count; cellIndex++)
                {
                    if ((Label)e.row.cell.cellindex == "EPreis") 
                    {
                        
                            //? Howto find all PReis and EPreis in row.count
                            //  is posible to put cell[x] and cell.text in array
                            // and at end of row sorting array 1 to x to colering
                            // the cell.

                        // Later to expand funktion Tolltip or mous over to show %
                        //   Diverent to cheeperst
                    }
                }
            }
        }
    }
} 

I found a solution but it still doesn't work properly.
The highest and lowest prices are sometimes reversed.

enter image description here

  if (e.Row.RowIndex >= 0)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int totalRow = mergeresult.Rows.Count;
                int totalColumns = mergeresult.Columns.Count;

                for (int i = 0; i < totalRow + 1; i++)
                {
                    //int anzahl = (int)ViewState["Anzahlfrom"];
                    //string[] NumOfPreisfields = new string[anzahl];
                    var Preisfields = new List<string>();
                    var Preisfieldslist = new List<(int cellNr, string Preis, string color)>();

                    for (int j = 0; j < e.Row.Cells.Count; j++)
                    {
                        var SearchColumnName = "EK";
                        DataControlFieldCell cell = (DataControlFieldCell)e.Row.Cells[j];

                        if (cell.ContainingField is BoundField)
                        {
                            if (((BoundField)cell.ContainingField).DataField.Contains(SearchColumnName))
                            {
                                Preisfields.Add(j.ToString());
                                Preisfieldslist.Add((j, cell.Text.ToString(), ""));
                                //cell.ForeColor = System.Drawing.Color.Red;
                            }
                        }

                        if (j == e.Row.Cells.Count -1)
                        {
                            //var SortedPreisfieldList = Preisfieldslist.OrderBy(r => r.Preis);
                            var LowerstPreis = Preisfieldslist.Min(r => r.Preis);
                             var HiegerstPreis = Preisfieldslist.Max(r => r.Preis);

                                 for (int c = 0; c < Preisfieldslist.Count; c++)
                                 {
                                     if (LowerstPreis == Preisfieldslist[c].Preis)
                                        {
                                        int n = Preisfieldslist[c].cellNr;
                                        e.Row.Cells[n].ForeColor = System.Drawing.Color.Green;
                                        }
                                        
                                        if (HiegerstPreis == Preisfieldslist[c].Preis)
                                        {
                                        int n = Preisfieldslist[c].cellNr;
                                        e.Row.Cells[n].ForeColor = System.Drawing.Color.Red;
                                 }
                            }
                        }
                    }
                }
            } // ende for (int i = 0; i < totalRow +1 ; i++)

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文