C#のgridViewの一般的なプロパティとテクニックの紹介
60449 ワード
1. GroupPanel
gridView1.OptionsView.ShowGroupPanel=false;
2.
sValue=Table.Rows[gridView1.FocusedRowHandle][FieldName].ToString();
3.
gridView1.OptionsBehavior.Editable=false;
4. MasterDetailView
gridView1.OptionsDetail.EnableMasterViewMode=false;
5. GroupPanel
gridView1.GroupPanelText=" ";
6. :
gridControl1.DataSource = dt;
FiledName--
Oracle , , ,sqlserver .
7.
:
ColumnViewOptionsBehavior.Editable = False
:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = True
:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = False
8. :
Columns , ColumnEdit.
LookUpEdit :
Designer In-Place Editor Repository LookUpEdit. Re1. . Columns 3 .Caption : , , .FieldName :FID,FNAME,FSEX. Re1 NullText .
AutoSearchColumnIndex 2.ImmediatePopup True.
SearchMode OnlyInPopup.
1( 1 ColumnEdit Re1)
Re1 .
Re1.DataSource = DALUse.Query("select fid,fname,fsex from dual").Tables[0];
Re1.DisplayMember = "FSEX";
Re1.ValueMember = "FNAME";
9.
gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
10. (Filter)
gridView1.Columns[0].OptionsFilter.AllowAutoFilter = false;
gridView1.Columns[0].OptionsFilter.AllowFilter = false;
gridView1.Columns[0].OptionsFilter.ImmediateUpdateAutoFilter = false;
11. ( )
gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left;
12. (0 0 )
string ss=gridView1.GetRowCellDisplayText(0, gridView1.Columns[0]);
string ss = gridView1.GetRowCellValue(0, gridView1.Columns[0]);
13. ( 0 0 123)
gridView1.SetRowCellValue(0, gridView1.Columns[0], "123");
13. dev
DevExpress.XtraGrid.Columns.GridColumn Col1=new DevExpress.XtraGrid.Columns.GridColumn ();
Col1.FieldName="FID";
Col1.Visible=true;
Col1.VisibleIndex=gridView1.Columns.Count;
gridView1.Columns.Add(Col1);
14. , gridview CustomDrawRowIndicator
private void gridview_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
15. : ( dgvdel datagridviewdel )
public static void datagridviewdel_Dev(DevExpress.XtraGrid.Views.Grid.GridView Mydgv)
{
if (MessageBox.Show(" ?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
{
int iSelectRowCount = Mydgv.SelectedRowsCount;
if (iSelectRowCount > 0)
{
Mydgv.DeleteSelectedRows();
}
}
}
16. : ( , AddNewRow )
private void btn_add_Click(object sender, EventArgs e)
{
gridView1.AddNewRow();
}
, gridView1_InitNewRow :
private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
{
ColumnView View = sender as ColumnView;
View.SetRowCellValue(e.RowHandle, View.Columns[0], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[0])); //
View.SetRowCellValue(e.RowHandle, View.Columns[1], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[1])); //
}
17. ( RefreshData RefreshDataSource , Dgvsave datagridviewsave , )
18. :gridcontrol 5 view , gridview, cardview、BandedView、Advanced BandedView、LayoutView; 5 。
1)、view OptionView viewmode “Carousel” “ ” gridcontrol view
2)、layoutView1.OptionsCarouselMode.PitchAngle “ ” pitch angle ; ; ; ; ;
3)、Roll Angle
4)、 , :
//
private void showData(List<Employee > list)
{
DataTable dt = new DataTable("OneEmployee");
dt.Columns.Add("Caption", System.Type.GetType("System.String"));
dt.Columns.Add("Department", System.Type.GetType("System.String"));
dt.Columns.Add("PhotoName", System.Type.GetType("System.Byte[]"));
for (int i = 0; i < list.Count; i++)
{
DataRow dr = dt.NewRow();
dr["Caption"] = list[i].Name;
dr["Department"] = list[i].Department;
string imagePath = @"D:\C#\photos" + list[i].PhotoPath;
dr["PhotoName"] = getImageByte(imagePath);
dt.Rows.Add(dr);
}
gridControl1.DataSource = dt;
}
// byte[]
private byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open);
byte[] imgByte = new byte [files.Length ];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
return imgByte;
}
19.
gridview ValidateRow :
#region
private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
{
GridView view = sender as GridView;
view.ClearColumnErrors();
if (view.GetRowCellValue(e.RowHandle, "ReceiveDate") == DBNull.Value)
{
e.Valid = false;
view.SetColumnError(view.Columns["ReceiveDate"], " ");
}
}
#endregion
gridview.UpdateCurrentRow()
DevExpress Winform 4 :
、GridControl
private void rILinkEditInfoDel_Click(object sender, EventArgs e)
{
if (XtraMessageBox.Show(" ?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
DataRow row = gvInfos.GetDataRow(gvInfos.FocusedRowHandle);
delByCode(row["Code"].ToString());
XtraMessageBox.Show(" !");
}
}
、
Hashtable ht = new Hashtable();
private void gridView6_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]);
if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" & View.GetRowCellValue(e.RowHandle, View.Columns["Value"]) != DBNull.Value)
{
decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"]));
object MinValue = View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]);
object MaxVlaue = View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]);
if (MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() != "" & MaxVlaue != DBNull.Value && MaxVlaue != null & MaxVlaue.ToString() != "")
{
decimal gridColumn2 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]));
decimal gridColumn1 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]));
if (gridColumn2 > AverValue || AverValue > gridColumn1)
{
if (!ht.ContainsKey("pic"))
ht.Add("pic", GetImage(1));
e.Value = ht["pic"];
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
byte[] GetImage(int key)
{
Image img = DevExpress.Utils.Controls.ImageHelper.CreateImageFromResources(string.Format("RiverSys.Resources.{0}.gif", key.ToString()), typeof(RiverInfos).Assembly);
return DevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(img, ImageFormat.Gif);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView6_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]);
if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" & View.GetRowCellValue(e.RowHandle, View.Columns["Value"]) != DBNull.Value)
{
decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"]));
object MinValue = View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]);
object MaxVlaue = View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]);
if (MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() != "" & MaxVlaue != DBNull.Value && MaxVlaue != null & MaxVlaue.ToString() != "")
{
decimal gridColumn2 = Convert.ToDecimal(MinValue);
decimal gridColumn1 = Convert.ToDecimal(MaxVlaue);
if (gridColumn2 > AverValue || AverValue > gridColumn1)
{
e.Appearance.ForeColor = Color.Red;
e.Appearance.BackColor = Color.LightGray;
}
}
}
}
}
、GridControl
private void gvMapColor_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
GridView view = sender as GridView;
DataView dv = view.DataSource as DataView;
if (e.IsGetData)
{
string strVal = dv[e.ListSourceRowIndex]["Color"].ToString();
if (strVal != "")
{
//e.Value = DevExpress.Utils.StyleLayout.ColorFromString(strVal);
e.Value = Common.HexToColor(strVal);
}
}
else
{
//Color colorVal = DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString());
Color colorVal = (Color)e.Value;
dv[e.ListSourceRowIndex]["Color"] = Common.RGB_HEX(colorVal.ToArgb());
}
}
、 GridControl
/// <summary>
/// GridView,
/// </summary>
/// <param name="parentId"></param>
private void GridViewBindData(string parentId)
{
this.gridView1.Columns.Clear();
this.FDs= areaSetupActionHelper.getDsRegionByParentId(parentId);
this.gridCArea.DataSource =this.FDs.Tables[0].DefaultView;
this.gridView1.Columns["id"].VisibleIndex = -1;
this.gridView1.Columns["childCounts"].VisibleIndex = -1;
this.gridView1.Columns["reg_id"].Caption = " ";
this.gridView1.Columns["reg_name"].Caption = " ";
this.gridView1.Columns["parent_id"].Caption = " ";
this.gridView1.Columns["reg_desc"].Caption = " ";
this.gridView1.Columns["parent_id"].ImageIndex =1;
this.gridView1.Columns["reg_desc"].ImageIndex = 0;
RepositoryItemTextEdit textEditReg_Id = new RepositoryItemTextEdit();
textEditReg_Id.Mask.EditMask =parentId+"file://d{2,3/}";
textEditReg_Id.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Regular;
this.gridView1.Columns["reg_id"].ColumnEdit = textEditReg_Id;
this.gridView1.Columns["reg_desc"].ColumnEdit = new RepositoryItemMemoExEdit();
TreeListNode node = this.treelArea.FocusedNode.ParentNode;
string fid = node==null?"0":node.GetValue("RegID").ToString().Trim();
DataSet ds = areaSetupActionHelper.getDsRegionByParentId(fid);
RepositoryItemLookUpEdit lookUEParent_Id = new RepositoryItemLookUpEdit();
lookUEParent_Id.Columns.Add(new LookUpColumnInfo("reg_id", 40, " "));
lookUEParent_Id.Columns.Add(new LookUpColumnInfo("reg_name", 40, " "));
lookUEParent_Id.DataSource = ds.Tables[0].DefaultView;
lookUEParent_Id.ValueMember = "reg_id";
lookUEParent_Id.DisplayMember = "reg_id";
this.gridView1.Columns["parent_id"].ColumnEdit = lookUEParent_Id;
}
/// <summary>
/// gridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
{
if (e.Valid == false&this.gridView1.FocusedColumn.FieldName == "reg_id")
{
e.ErrorText = " !
2~3 !";
}
if (this.gridView1.FocusedColumn.FieldName == "reg_name")
{
Regex reg=new Regex(@"[\u4e00-\u9fa5]{1,20}");
Match m=reg.Match(e.Value.ToString().Trim());
if (m.Length != e.Value.ToString().Trim().Length)
{
e.Valid = false;
e.ErrorText = "
1 20";
}
}
}
private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e)
{
if (MyDialog.Alert("
?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
e.ExceptionMode = ExceptionMode.Ignore;
}
}
/// <summary>
/// gridView
/// </summary>
private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
{
string regid = this.gridView1.GetRowCellValue(e.RowHandle, "reg_id").ToString().Trim();
string regName = this.gridView1.GetRowCellValue(e.RowHandle, "reg_name").ToString().Trim();
if ( regid.Length < 1)
{
e.Valid = false;
this.gridView1.SetColumnError(this.gridView1.Columns["reg_id"], " !", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
// e.ErrorText = " !";
}
if (regName.Length < 1)
{
e.Valid = false;
this.gridView1.SetColumnError(this.gridView1.Columns["reg_name"], " !", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
}
}
private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
{
if (e.RowHandle >= 0)
{
if (this.gridView1.GetRowCellValue(e.RowHandle, this.gridView1.Columns["reg_id"]).ToString().Trim() == "" || this.gridView1.GetRowCellValue(e.RowHandle, this.gridView1.Columns["reg_name"]).ToString().Trim() == "")
{
if (MyDialog.Alert("
?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
e.ExceptionMode = ExceptionMode.Ignore;
}
else
{
e.ExceptionMode = ExceptionMode.NoAction;
}
}
}
else
{
e.ExceptionMode = ExceptionMode.Ignore;
}
}
XtraGrid (GridControl ) :51 :2010-04-19Hits:146
DevExpress XtraGrid (AspxGridControl )
: gridView_bcode.FocusedRowHandle = focuseRowInt; // GridView FocusedRowHandle
view plaincopy to clipboardprint?
//
ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;// ID id
int focusedhandle = cv.FocusedRowHandle;
object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id");
if (DBNull.Value != rowIdObj)
{
FocusedRow_id = Convert.ToInt32(rowIdObj);
}
//
ColumnView cv = (ColumnView)gridControl_Gongzi.FocusedView;// ID id
int focusedhandle = cv.FocusedRowHandle;
object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "id");
if (DBNull.Value != rowIdObj)
{
FocusedRow_id = Convert.ToInt32(rowIdObj);
}
view plaincopy to clipboardprint?
//
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
int intRowHandle = e.RowHandle;
FocusedRow_bumen = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "bumen"));
FocusedRow_xingming = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "xingming"));
//FocusedRow_jibengongzi = Convert.ToDecimal(gridView1.GetRowCellValue(intRowHandle, "jibengongzi"));
object rowJibengongziObj = gridView1.GetRowCellValue(intRowHandle, "jibengongzi");
if (DBNull.Value != rowJibengongziObj)
{
FocusedRow_jibengongzi = Convert.ToDecimal(rowJibengongziObj);
}
}
//
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
int intRowHandle = e.RowHandle;
FocusedRow_bumen = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "bumen"));
FocusedRow_xingming = Convert.ToString(gridView1.GetRowCellValue(intRowHandle, "xingming"));
//FocusedRow_jibengongzi = Convert.ToDecimal(gridView1.GetRowCellValue(intRowHandle, "jibengongzi"));
object rowJibengongziObj = gridView1.GetRowCellValue(intRowHandle, "jibengongzi");
if (DBNull.Value != rowJibengongziObj)
{
FocusedRow_jibengongzi = Convert.ToDecimal(rowJibengongziObj);
}
} view plaincopy to clipboardprint?
//
ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView;
view.FocusedColumn = view.Columns["bumen"];
//
ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView;
view.FocusedColumn = view.Columns["bumen"]; view plaincopy to clipboardprint?
// id
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
int intRowHandle = e.FocusedRowHandle;
object rowIdObj = gridView1.GetRowCellValue(intRowHandle, "id");
if (DBNull.Value != rowIdObj)// id
{
FocusedRow_id = Convert.ToInt32(rowIdObj);
}
}
// id
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
int intRowHandle = e.FocusedRowHandle;
object rowIdObj = gridView1.GetRowCellValue(intRowHandle, "id");
if (DBNull.Value != rowIdObj)// id
{
FocusedRow_id = Convert.ToInt32(rowIdObj);
}
}
view plaincopy to clipboardprint?
// FocusedHandle :
FocuseRow_Handle = -999998;
// handle
ColumnView newview = (ColumnView)gridControl_Gongzi.FocusedView;
FocuseRow_Handle = newview.FocusedRowHandle;
//
private void gridView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
ColumnView view = (ColumnView)gridControl_Gongzi.FocusedView;
if(view.IsLastRow)
{
if (FocuseRow_Handle == 0)
{
gridView1.AddNewRow();
ColumnView newview = (ColumnView)gridControl_Gongzi.FocusedView;
newview.FocusedColumn = newview.Columns["bumen"];//
FocuseRow_Handle = newview.FocusedRowHandle;// FocuseRowHandle FocuseRow_Handle update insert
C#のGridViewフォーマット文字列は、実はDataFromatStringを使用する方法です.では、具体的な内容と実装例のアプリケーションを紹介します.
ビジネス・ロジック・レイヤからデータ・エンティティを取得すると、次のことはコントロールにバインドされます.データエンティティの一部のフィールドは、インタフェースに直接バインドできますが、フォーマットを再フォーマットする必要があるフィールドもあります.たとえば、通貨単位フィールドでは、通貨記号を表示し、3桁ごとに区切り記号を表示する必要があります.たとえば日付フィールドでは、データベースに日付と時刻が格納されていますが、インタフェースにはXXXX年XX月XX日のフォーマットで表示する必要があります.このとき、DataFormatStringプロパティを使用します.
﹤asp:GridView ID="grvResult" runat="server"
AutoGenerateColumns="False" Width="100%"﹥
﹤Columns﹥
﹤asp:BoundField HeaderText=" "
DataField="OperationDate" DataFormatString="{
0:yyyy-MM-dd}" HtmlEncode="False"﹥
﹤/asp:BoundField﹥
﹤asp:BoundField HeaderText=" "
DataField="TotalRate" DataFormatString="{
0:C}" HtmlEncode="False"﹥
﹤/asp:BoundField﹥
﹤/Columns﹥
﹤/asp:GridView﹥
たとえば、上記のコードは、日付と通貨の2つのバインド方法を示しています.DataFormatStringの{0}は、Stringと固定されたフォーマットです.Fromat("{0}",someString)の{0}は、バインドコンテキストのパラメータインデックス番号を表す使用法です.その後、後でC#フォーマット文字列を追加し、具体的な使い方はMSDNを参照してください.
C#のGridViewフォーマット文字列注意以下の点
1. GridViewのasp:BoundFieldでDataFormatStringを使用するには、属性HtmlEncode="False"を設定する必要があります.そうしないと機能しません.
2. 日付タイプのフォーマット文字列を使用する必要がある場合は、データエンティティの対応するフィールドも日からタイプする必要があります.
3. フォーマット文字列Cは通貨単位を表し、バインドするデータ型は数値型であるべきである.文字列タイプが機能しない場合は、DataFormatString="¥{0:C}"のフォーマット文字列を手動で追加する必要があります.
C#のGridViewフォーマット文字列の基本的な状況をここに紹介します.C#のGridViewフォーマット文字列の学習と理解に役立ちます.
1、 gridview “ ” , ASP , ID , , , ASP.net , 。
2、 LinkButton , , 。 (gridview.selectedRow ), 。
CommandName, CommandArgument LinkButton CommandArgument 。
CommandName LinkButton 。
CommandArgument( ID ), 。
swicth(CommandName)
case "sort":{}
case “delete”:{}