将新列添加到GridView DevExpress

时间:2013-01-28 12:23:02

标签: 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控件..如何在add中添加此列 对不起,我是DevExpress的新手 谢谢..

1 个答案:

答案 0 :(得分:6)

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

将UnBound列添加到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;
   }
}

希望这有帮助..