[英]add new column to a GridView 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 ,您可以在其中編寫自定義文本。
將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 = "";
}
要了解如何創建列並將它們綁定到數據字段 ,請參閱文檔:
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;
}
}
希望這有幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.