c# - 将新列添加到 GridView DevExpress

标签 c# winforms excel devexpress

我正在从 Excel 导入工作表。 我需要在网格的末尾添加一个新单元格,其中包含有关我将它们保存在名为 msg 的数组中的空单元格的消息;

    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);
        }   

        string[] msg = new string[50];
        for (int i = 0; i < gridView3.RowCount ; i++)
        {
            System.Data.DataRow Rows = gridView3.GetDataRow(i);
            string cellvalue = Rows[0].ToString();
            if (cellvalue == "")
            {
                msg[0] = "Missing 'First Name'";
            }
            cellvalue = Rows[1].ToString();
            if (cellvalue == "")
            {
                msg[1] = "Missing 'Father Name'";
            }
            cellvalue = Rows[2].ToString();
            if (cellvalue == "")
            {
                msg[2] = "Missing 'Last Name'";
            }       
        }

我正在使用 XtraGrid 控件。我如何将此列添加到添加中 对不起,我是 DevExpress 的新手 谢谢..

最佳答案

我建议您使用 XtraGrid Unbound Columns ,您可以在其中编写自定义文本。

将未绑定(bind)列添加到 Xtragrid 的示例代码片段:

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;

private void Form1_Load(object sender, System.EventArgs e) {
   // ...
   gridControl1.ForceInitialize();

   // Create an unbound column.
   GridColumn unbColumn = gridView1.Columns.AddField("Total");
   unbColumn.VisibleIndex = gridView1.Columns.Count;
   unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
   // Disable editing.
   unbColumn.OptionsColumn.AllowEdit = false;
   // Specify format settings.
   unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
   unbColumn.DisplayFormat.FormatString = "c";
   // Customize the appearance settings.
   unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;
}

// Returns the total amount for a specific row.
decimal getTotalValue(int listSourceRowIndex) {
    DataRow row = nwindDataSet.Tables["Order Details"].Rows[listSourceRowIndex];
    decimal unitPrice = Convert.ToDecimal(row["UnitPrice"]);
    decimal quantity = Convert.ToDecimal(row["Quantity"]);
    decimal discount = Convert.ToDecimal(row["Discount"]);
    return unitPrice * quantity * (1 - discount);
}

// Provides data for the Total column.
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
   if (e.Column.FieldName == "Total" && e.IsGetData) e.Value = 
     getTotalValue(e.ListSourceRowIndex);
}

或者您可以使用 CustomColumnDisplayText显示自定义文本 Xtragrid 的事件。

using DevExpress.XtraGrid.Views.Base;

    private void gridView1_CustomColumnDisplayText(object sender, 
    CustomColumnDisplayTextEventArgs e) {
       if(e.Column.FieldName == "Discount")
          if(Convert.ToDecimal(e.Value) == 0) e.DisplayText = "";
    }

要知道how create Columns and Binding Them to Data Fields ,引用文档:

Overview of Columns and Card Fields

using DevExpress.XtraGrid.Views.Base;

ColumnView View = gridControl1.MainView as ColumnView;
DialogResult answer = MessageBox.Show("Do you want to create columns for all fields?", 
  "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
   View.PopulateColumns();
else {
   string[] fieldNames = new string[] {"ProductID", "ProductName", "QuantityPerUnit", "UnitPrice"};
   DevExpress.XtraGrid.Columns.GridColumn column;
   View.Columns.Clear();
   for (int i = 0; i < fieldNames.Length; i++) {
      column = View.Columns.AddField(fieldNames[i]);
      column.VisibleIndex = i;
   }
}

希望这有帮助..

关于c# - 将新列添加到 GridView DevExpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561965/

相关文章:

c# - 禁用和隐藏 TabPage

c# - 如何动态填充 CheckedListBox?

c# - 组合框的 SelectedValue 返回意外值

Excel VBA - 根据值更改单元格颜色

c# - 如何获取路径文件夹浏览器对话框

c# - 返回 HttpResponseMessage 时的 WebAPI Gzip

未完全应用任何大小矩阵(1x1、3x3、5x5,...)的 C# 卷积过滤器

python - 无法写入 excel AttributeError : 'Worksheet' object has no attribute 'write'

excel - 在网站上集成 pdf excel 单词查看器

c# - 在 C# 中按片假名对日语文本进行排序