【问题标题】:GridView DevExpress check if any cell is nullGridView DevExpress 检查是否有任何单元格为空
【发布时间】:2013-01-28 10:10:37
【问题描述】:

我需要检查一个单元格是否为空并存储一条消息,然后在包含所有消息的每一行中创建一个新单元格

但我不知道如何使用 DevExpress 可以帮助我的代码

    string Name = "First Name";
    string FName = "Father Name";
    string LName = "Last Name";
    string EmpCode = "Employee Code";
    string Tax = "Tax#";
    string SocSec = "Soc.Sec#";
    string EmpType = "Employment Type";
    string DepCode = "Department Code";
    string DepDesc = "Department Description";
    private void simpleButton1_Click(object sender, System.EventArgs e)
    {
        try
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\Emp.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\"";

            con.Open();
            DataTable dtSchema;
            dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", con);
            OleDbDataAdapter da = new OleDbDataAdapter(Command);
            DataSet ds = new DataSet ();
            da.Fill(ds);
            dataGrid1.DataSource = ds.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

这个循环没有给我关于空单元格的正确信息我认为我写的内容不正确也许还有另一种更好的方法......

        for (int rows = 0 ; rows < gridView3.RowCount ; rows ++)
        {
            string[] msg = new string[50];

            if ((gridView3.GetRowCellValue(rows, gridView3.Columns["LName"])) == null)
            {
                msg[rows] = "name missing";
            }
        }

【问题讨论】:

标签: c# devexpress


【解决方案1】:

据我所知,单元格的值不会为空。然而,底层数据源将有一个空值。

检查单元格的数据是否为空或从空值求和:

private bool IsNullValue(PivotDrillDownDataSource ds, PivotGridField field)
{
    if (ds.RowCount == 0 || field == null) return false;
    for (int i = 0; i < ds.RowCount; i++)
    {
        if (Equals(ds[i][field], null))
        return true;
    }
    return false;
 } 

更改空单元格中的文本:

if (IsNullValue(e.CreateDrillDownDataSource(), e.DataField))
     e.DisplayText = "NULL OR SUM WITH NULL";

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,下面的代码对我来说运行良好,只是与 OP 尝试的略有不同。我希望它会对某人有所帮助。

    for (int rows = 0 ; rows < gridView3.RowCount ; rows ++)
        {
            string[] msg = new string[50];
    
            if ((gridView3.GetRowCellValue(rows, "LName")) == null)
            {
                msg[rows] = "name missing";
            }
        }
    

    LName 需要替换成字符串 fieldName。

    【讨论】:

      【解决方案3】:
      private void gridViewPro_RowStyle(object sender, 
          DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
      {
          GridView View = sender as GridView;
          if (e.RowHandle >= 0)
          {
              double Sale = Convert.ToDouble(
                  View.GetRowCellDisplayText(e.RowHandle, View.Columns["PRO_S3M"]));
              double Qua = Convert.ToDouble(
                  View.GetRowCellDisplayText(e.RowHandle, View.Columns["PRO_QTY"]));
              if (Sale > 0 && Qua > 0)
              {
                  if (Sale >= Qua)
                  {
                      e.Appearance.BackColor = Color.OrangeRed;
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        使用 AspxGridView 的这个事件

         protected void grid_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
        {        // Get Cell Values here and compare as per your conditions
            string text = e.CellValue.ToString();                   
         }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多