精华内容
下载资源
问答
  • 在项目中经常遇到要动态生成控件动态显示指定的控件或者隐藏,这就要涉及到查找控件的问题。 下面介绍下Winform中使用控件名来查找窗体或者容器中的控件的方法,代码如下: /// <summary> /// 通过控件...

            在项目中经常遇到要动态生成控件,动态显示指定的控件或者隐藏,这就要涉及到查找控件的问题。

           下面介绍下Winform中使用控件名来查找窗体或者容器中的控件的方法,代码如下:

            /// <summary>
                /// 通过控件名获取控件
                /// </summary>
                /// <param name="strName"></param>
                /// <returns></returns>
                private Control GetPbControl(string strName)
                {
                    string pbName = strName;
                    return GetControl(this, pbName);
                }
                /// <summary>
                /// 通过控件名获取控件
                /// </summary>
                /// <param name="ct">控件所在的容器或者窗体</param>
                /// <param name="name">需要查找的控件名</param>
                /// <returns></returns>
                public static Control GetControl(Control ct, string name)
                {
                    Control[] ctls = ct.Controls.Find(name, false);
                    if (ctls.Length > 0)
                    {
                        return ctls[0];
                    }
                    else
                    {
                        return null;
                    }
                }

               查找控件的 Demo代码如下:

                strPxName = "SoftKeyBoard";
                    Control sk = GetPbControl(strPxName);
                    if (sk == null)
                    {
                        return;
                    }

     

     

    转载于:https://www.cnblogs.com/kevinGao/archive/2011/10/25/2224217.html

    展开全文
  • 寒假因为疫情不能出去,在家闲着没事想破解下邻居WiFi又没有合适的字典,有些字典生成器功能是全面但有些需求无法实现,...有一个需求是要根据用户在输入框中输入的值,来动态的显示/隐藏一定数量的单选按钮,单选按...

    寒假因为疫情不能出去,在家闲着没事想破解下邻居WiFi又没有合适的字典,有些字典生成器功能是全面但有些需求无法实现,大一简单学了下C#,索性自己就用winform编写个字典生成器,但因为对winform了解的不是很透彻,难免会走很多弯路,接下来就把其中一个问题及其解决方案分享给大家,也算是给自己做个笔记。

    需求

    有一个需求是要根据用户在输入框中输入的值,来动态的显示/隐藏一定数量的单选按钮,单选按钮排列在panel框架中。如下图:
    字典生成器

    解决方案一

    笨方法,不推荐
    因为对winform并不是多精通,一开始想的是用switch来根据输入框中获取的值来一个一个改变单选按钮(RadioButton)的Visible属性,如下面代码所示:

    private void text1_TextChanged(object sender, EventArgs e)
    {
    	int n;
    	try
        	{
        		n = int.Parse(text1.Text);
        	}	
    	catch
        	{
            	n = 0;
        	}
    	switch(n)
    	{
    		case 1:
    			RadioButtun1.Visible = true;
    			RadioButtun2.Visible = false;
    			RadioButtun3.Visible = false;
    			...
    			break;
    		case 2:
    			RadioButtun1.Visible = true;
    			RadioButtun2.Visible = true;
    			RadioButtun3.Visible = false;
    			...
    			break;
    		...
    	}
    }
    

    这样写的确可以满足需求,但是实在太笨了,并且我的界面里有13个单选按钮,每一种情况都要写大量的重复代码太费事费力,也不符合简洁、高效的编程思想,只是提供一种解决问题的思路。后来查阅资料后有了更加合理的解决方案,请看解决方案二。

    解决方案二

    查阅资料了解到了winform中的Control用法,可以批量控制窗体中的控件。
    所以解决方法是:用Control来遍历panel中的所有单选按钮(RadioButtun),用一个变量i来获取RadioButtun中的Text内容,再通过一个if语句来控制哪些单选按钮显示,哪些需要隐藏。
    最后代码如下:

    private void text1_TextChanged(object sender, EventArgs e)
    {
    	int n,i;
    	try
    	{
        	n = int.Parse(text1.Text);
    	}
    	catch
    	{
        	n = 0;
    	}
    	foreach (Control control in this.panel1.Controls)
    	{
        	i = int.Parse(control.Text);
        	if (control is RadioButton)
        	{
            	if (i<=n)
            	{
                	control.Visible = true;
            	}
            	else
            	{
                	control.Visible = false;
            	}
        	}
    	}
    }
    

    最后结果如下:
    最终结果1
    最终结果2
    这样就用很简洁的代码实现了想要的功能,当然肯定还有其他的方法,大家也可以在下面讨论。

    小记: 在将输入框中的字符串转换成int类型时,一定要用try对异常情况进行处理,否则当用户将输入框中的内容清空时VS会报错。

    总结

    在这个示例中,也可以通过改变代码来控制其他的控件或实现其他的功能。在大学里学到的知识毕竟有限,由于对各种语言学习的不透彻导致弯路不断,所以查阅资料的能力很关键,可以有效地帮助我们解决问题,当然编程最重要的还是多写多练,没有什么捷径。这也是我第一次在CSDN上写文章,难免会有不足之处,还请大家多多指教。最后祝大家新年快乐!身体健康!

    展开全文
  • 1、自定义控件增加智能标签; 2、智能标签中增加新增菜单组的命令; 3、使用时,自定义控件拖到设计窗口中,点击增加菜单组后自动生成一个按钮和一个Panel; 4、自动生成的按钮,可以在设计时点击响应函数,对...
  • 主要介绍了WinForm实现页面按钮定时隐藏功能,结合实例形式分析了WinForm基于定时器的页面控件属性动态操作相关实现技巧,需要的朋友可以参考下
  • 上篇关于 “在界面中动态修改列显示,列名列宽等”有缺陷,例如:GridView的DataSource是联合查询,就要手工修改XML文件,不支持XtraGrid列宽的拖拉,于是进一步改进。思路如下:在GridControl第一次载入的时候,用...

    上篇关于 “在界面中动态修改列显示,列名列宽等”有缺陷,例如:GridView的DataSource是联合查询,就要手工修改XML文件,不支持XtraGrid列宽的拖拉,于是进一步改进。

    思路如下:在GridControl第一次载入的时候,用XML文件记录GridView的列的信息。关闭窗体的时候,再次保存列的信息。列的隐藏,固定在弹出窗体中设定。

    用到的一些方法入下:
    //根据XML文件保存的信息设置GridView中的列
    public static void SetGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
            {
                gridView.Columns.Clear();

                DataSet ds = new DataSet();
                ds.ReadXml(xmlFile);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
                    gridCoulumn.Caption = ds.Tables[0].Rows[i]["Caption"].ToString();
                    gridCoulumn.FieldName = ds.Tables[0].Rows[i]["FieldName"].ToString();
                    gridCoulumn.Name = ds.Tables[0].Rows[i]["ColumnName"].ToString();
                    gridCoulumn.VisibleIndex = int.Parse(ds.Tables[0].Rows[i]["VisibleIndex"].ToString());
                    gridCoulumn.Visible = ds.Tables[0].Rows[i]["Visible"].ToString().ToLower() == "true" ? true : false;
                    if (!gridCoulumn.Visible)
                    {
                        gridCoulumn.VisibleIndex = -1;
                    }
                    gridCoulumn.Width = int.Parse(ds.Tables[0].Rows[i]["Width"].ToString());
                    switch (ds.Tables[0].Rows[i]["Fixed"].ToString().ToLower())
                    {
                        case "none":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                            break;
                        case "left":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
                            break;
                        case "right":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
                            break;
                    }
                    if (ds.Tables[0].Rows[i]["ColumnEdit"].ToString() == "RepositoryItemLookUpEdit")
                    {
                    }
                    gridCoulumn.OptionsColumn.ReadOnly = true;
                    //gridCoulumn.BestFit();

                    gridView.Columns.Add(gridCoulumn);
                }
            }

          
      public static void GridStyleShow(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
            {
                gridView.Columns.Clear();

                DataTable dt = new DataTable();
                dt.TableName = "XMLStyle";
                dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("FieldName", typeof(string)),
                        new DataColumn("Caption", typeof(string)),
                        new DataColumn("ColumnName", typeof(string)),
                        new DataColumn("Visible", typeof(bool)),
                        new DataColumn("VisibleIndex", typeof(int)),
                        new DataColumn("Width", typeof(int)),
                        new DataColumn("Fixed", typeof(string)),
                        new DataColumn("ReadOnly", typeof(bool))
                    });

                dt.ReadXml(xmlFile);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
                        gridCoulumn.FieldName = dt.Rows[i]["FieldName"].ToString();
                        gridCoulumn.Caption = dt.Rows[i]["Caption"].ToString();
                        gridCoulumn.Name = dt.Rows[i]["ColumnName"].ToString();
                        gridCoulumn.Visible = (bool)dt.Rows[i]["Visible"];
                        gridCoulumn.VisibleIndex = (int)dt.Rows[i]["VisibleIndex"];
                        gridCoulumn.Width = (int)dt.Rows[i]["Width"];

                        switch (dt.Rows[i]["Fixed"].ToString().ToUpper())
                        {
                            case "LEFT":
                                gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
                                break;
                            case "RIGHT":
                                gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
                                break;
                            case "NONE":
                                gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                                break;
                            default:
                                break;
                        }

                        gridCoulumn.OptionsColumn.ReadOnly = (bool)dt.Rows[i]["ReadOnly"];
                        gridView.Columns.Add(gridCoulumn);
                    }
            }

            public static void GridStyleSave(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
            {
                DataTable dt = GridStyleGet(gridView, xmlFile);
                DataTableToXML(dt, xmlFile); 
            }

            public static void DataTableToXML(DataTable dt, string xmlFile)
            {
                DataTable dtTemp = TableSort(dt, "VisibleIndex asc");

                System.IO.FileStream fs = new System.IO.FileStream(xmlFile, System.IO.FileMode.Create);
                System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Default);
                xw.WriteStartDocument();
                dtTemp.WriteXml(xw);

                xw.Close();
                fs.Close();
            }

            public static DataTable GridStyleGet(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
            {
                DataTable dt = new DataTable();
                dt.TableName = "XMLStyle";
                dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("FieldName", typeof(string)),
                        new DataColumn("Caption", typeof(string)),
                        new DataColumn("ColumnName", typeof(string)),
                        new DataColumn("Visible", typeof(bool)),
                        new DataColumn("VisibleIndex", typeof(int)),
                        new DataColumn("Width", typeof(int)),
                        new DataColumn("Fixed", typeof(string)),
                        new DataColumn("ReadOnly", typeof(bool))
                    });

                for (int i = 0; i < gridView.Columns.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["FieldName"] = gridView.Columns[i].FieldName;
                    dr["Caption"] = gridView.Columns[i].Caption;
                    dr["ColumnName"] = gridView.Columns[i].Name;
                    dr["Visible"] = gridView.Columns[i].Visible;
                    dr["VisibleIndex"] = gridView.Columns[i].VisibleIndex;
                    dr["Width"] = gridView.Columns[i].Width;

                    switch (gridView.Columns[i].Fixed)
                    {
                        case DevExpress.XtraGrid.Columns.FixedStyle.Left:
                            dr["Fixed"] = "Left";
                            break;
                        case DevExpress.XtraGrid.Columns.FixedStyle.Right:
                            dr["Fixed"] = "Right";
                            break;
                        case DevExpress.XtraGrid.Columns.FixedStyle.None:
                            dr["Fixed"] = "None";
                            break;
                        default:
                            dr["Fixed"] = "None";
                            break;
                    }

                    dr["ReadOnly"] = gridView.Columns[i].OptionsColumn.ReadOnly;

                    dt.Rows.Add(dr);
                }
                return dt;
            }

            public static DataTable GridFixedType()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FixShow", typeof(string));
                dt.Columns.Add("FixName", typeof(string));

                DataRow dr1 = dt.NewRow();
                dr1[0] = "不固定";
                dr1[1] = "None";
                dt.Rows.Add(dr1);

                DataRow dr2 = dt.NewRow();
                dr2[0] = "左邊";
                dr2[1] = "Left";
                dt.Rows.Add(dr2);

                DataRow dr3 = dt.NewRow();
                dr3[0] = "右邊";
                dr3[1] = "Right";
                dt.Rows.Add(dr3);

                return dt;
            }

            public static DataTable TableSort(DataTable dt,string sort)
            {
                DataRow[] resultRows= dt.Select(null, sort);
                DataTable tableTemp = dt.Copy();
                tableTemp.Rows.Clear();
                foreach (DataRow dr in resultRows)
                {
                    object[] objItem = dr.ItemArray;
                    DataRow row = tableTemp.NewRow();
                    row.ItemArray = objItem;
                    tableTemp.Rows.Add(row);
                }
                return tableTemp;
            }

    栏目设置窗体代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace ProcessPay
    {
        public partial class FrmColumnSetup : ProcessPay.FrmBase
        {
            public FrmColumnSetup()
            {
                InitializeComponent();
            }

            public delegate void RefreshEventHandler(string strFilter);
            public event RefreshEventHandler RefreshData;

            private DataTable dt;
            private string xmlFile;

            public DataTable DT
            {
                set { dt = value; }
                get { return dt; }
            }

            public string XmlFile
            {
                set { xmlFile = value; }
                get { return xmlFile; }
            }

            private void FrmColumnSetup_Load(object sender, EventArgs e)
            {
                repositoryItemLookUpEdit1.DataSource = Common.GridFixedType();

                gridControl1.DataSource = dt;
            }

            private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
            {
                string fieldName = e.Column.FieldName;
                int rowHandle = e.RowHandle;
                if (fieldName == "Visible")
                {
                    if ((bool)gridView1.GetRowCellValue(rowHandle, fieldName))
                    {
                        int maxIndex = 0;
                        for (int i = 0; i < gridView1.RowCount; i++)
                        {
                            if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > maxIndex)
                            {
                                maxIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString());
                            }
                        }
                        gridView1.SetRowCellValue(rowHandle, "VisibleIndex", maxIndex + 1);
                    }
                    else
                    {
                        int visibleIndex = int.Parse(gridView1.GetRowCellValue(rowHandle, "VisibleIndex").ToString());
                        for (int i = 0; i < gridView1.RowCount; i++)
                        {
                            if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > visibleIndex)
                            {
                                int tempIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) - 1;
                                gridView1.SetRowCellValue(i, "VisibleIndex", tempIndex);
                            }
                        }
                        gridView1.SetRowCellValue(rowHandle, "VisibleIndex", -1);
                    }
                }
                gridView1.CloseEditor();
            }

            private void SBSave_Click(object sender, EventArgs e)
            {
                Common.DataTableToXML(dt, xmlFile);
                if (RefreshData != null)
                {
                    RefreshData("");
                }
                this.Close();
            }
        }
    }

    主窗体代码:

    private void FrmMain_Load(object sender, EventArgs e)
            {
                BindDataStyle("");
            }

            private void BindDataStyle(string strFilter)
            {
                bool isExist = System.IO.File.Exists(xmlFile);
                if (isExist)
                {
                    Common.GridStyleShow(gridView1, xmlFile);
                }

                BindData(strFilter);

                if (!isExist)
                {
                    Common.GridStyleSave(gridView1, xmlFile);
                }
            }

            private void BindData(string strFilter)
            {
                DataSet ds = (new ProcessPay.BLL.SetNo()).GetList();
                gridControl1.DataSource = ds.Tables[0];
            }

            private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
                Common.GridStyleSave(gridView1, xmlFile);
            }
            //逆ヘ砞竚
            private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
            {
                FrmColumnSetup frm = new FrmColumnSetup();

                DataTable dt = Common.GridStyleGet(gridView1, xmlFile);
                frm.DT = dt;
                frm.XmlFile = xmlFile;
                frm.RefreshData += new FrmColumnSetup.RefreshEventHandler(BindDataStyle);
                frm.ShowDialog();
            }

    展开全文
  • 上篇关于“在界面中动态修改列显示,列名列宽等”有缺陷,例如:GridView的DataSource是联合查询,就要手工修改XML文件,不支持XtraGrid列宽的拖拉,于是进一步改进。 思路如下:在GridControl第一次载入的时候,用...

    上篇关于“在界面中动态修改列显示,列名列宽等”有缺陷,例如:GridView的DataSource是联合查询,就要手工修改XML文件,不支持XtraGrid列宽的拖拉,于是进一步改进。

    思路如下:在GridControl第一次载入的时候,用XML文件记录GridView的列的信息。关闭窗体的时候,再次保存列的信息。列的隐藏,固定在弹出窗体中设定。

    用到的一些方法入下:
    //根据XML文件保存的信息设置GridView中的列
    public static void SetGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
    {
    gridView.Columns.Clear();

    DataSet ds = new DataSet();
    ds.ReadXml(xmlFile);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
    gridCoulumn.Caption = ds.Tables[0].Rows[i]["Caption"].ToString();
    gridCoulumn.FieldName = ds.Tables[0].Rows[i]["FieldName"].ToString();
    gridCoulumn.Name = ds.Tables[0].Rows[i]["ColumnName"].ToString();
    gridCoulumn.VisibleIndex = int.Parse(ds.Tables[0].Rows[i]["VisibleIndex"].ToString());
    gridCoulumn.Visible = ds.Tables[0].Rows[i]["Visible"].ToString().ToLower() == "true" ? true : false;
    if (!gridCoulumn.Visible)
    {
    gridCoulumn.VisibleIndex = -1;
    }
    gridCoulumn.Width = int.Parse(ds.Tables[0].Rows[i]["Width"].ToString());
    switch (ds.Tables[0].Rows[i]["Fixed"].ToString().ToLower())
    {
    case "none":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
    break;
    case "left":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
    break;
    case "right":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
    break;
    }
    if (ds.Tables[0].Rows[i]["ColumnEdit"].ToString() == "RepositoryItemLookUpEdit")
    {
    }
    gridCoulumn.OptionsColumn.ReadOnly = true;
    //gridCoulumn.BestFit();

    gridView.Columns.Add(gridCoulumn);
    }
    }


    public static void GridStyleShow(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
    {
    gridView.Columns.Clear();

    DataTable dt = new DataTable();
    dt.TableName = "XMLStyle";
    dt.Columns.AddRange(new DataColumn[] {
    new DataColumn("FieldName", typeof(string)),
    new DataColumn("Caption", typeof(string)),
    new DataColumn("ColumnName", typeof(string)),
    new DataColumn("Visible", typeof(bool)),
    new DataColumn("VisibleIndex", typeof(int)),
    new DataColumn("Width", typeof(int)),
    new DataColumn("Fixed", typeof(string)),
    new DataColumn("ReadOnly", typeof(bool))
    });

    dt.ReadXml(xmlFile);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
    gridCoulumn.FieldName = dt.Rows[i]["FieldName"].ToString();
    gridCoulumn.Caption = dt.Rows[i]["Caption"].ToString();
    gridCoulumn.Name = dt.Rows[i]["ColumnName"].ToString();
    gridCoulumn.Visible = (bool)dt.Rows[i]["Visible"];
    gridCoulumn.VisibleIndex = (int)dt.Rows[i]["VisibleIndex"];
    gridCoulumn.Width = (int)dt.Rows[i]["Width"];

    switch (dt.Rows[i]["Fixed"].ToString().ToUpper())
    {
    case "LEFT":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
    break;
    case "RIGHT":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
    break;
    case "NONE":
    gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
    break;
    default:
    break;
    }

    gridCoulumn.OptionsColumn.ReadOnly = (bool)dt.Rows[i]["ReadOnly"];
    gridView.Columns.Add(gridCoulumn);
    }
    }

    public static void GridStyleSave(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
    {
    DataTable dt = GridStyleGet(gridView, xmlFile);
    DataTableToXML(dt, xmlFile);
    }

    public static void DataTableToXML(DataTable dt, string xmlFile)
    {
    DataTable dtTemp = TableSort(dt, "VisibleIndex asc");

    System.IO.FileStream fs = new System.IO.FileStream(xmlFile, System.IO.FileMode.Create);
    System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Default);
    xw.WriteStartDocument();
    dtTemp.WriteXml(xw);

    xw.Close();
    fs.Close();
    }

    public static DataTable GridStyleGet(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
    {
    DataTable dt = new DataTable();
    dt.TableName = "XMLStyle";
    dt.Columns.AddRange(new DataColumn[] {
    new DataColumn("FieldName", typeof(string)),
    new DataColumn("Caption", typeof(string)),
    new DataColumn("ColumnName", typeof(string)),
    new DataColumn("Visible", typeof(bool)),
    new DataColumn("VisibleIndex", typeof(int)),
    new DataColumn("Width", typeof(int)),
    new DataColumn("Fixed", typeof(string)),
    new DataColumn("ReadOnly", typeof(bool))
    });

    for (int i = 0; i < gridView.Columns.Count; i++)
    {
    DataRow dr = dt.NewRow();
    dr["FieldName"] = gridView.Columns[i].FieldName;
    dr["Caption"] = gridView.Columns[i].Caption;
    dr["ColumnName"] = gridView.Columns[i].Name;
    dr["Visible"] = gridView.Columns[i].Visible;
    dr["VisibleIndex"] = gridView.Columns[i].VisibleIndex;
    dr["Width"] = gridView.Columns[i].Width;

    switch (gridView.Columns[i].Fixed)
    {
    case DevExpress.XtraGrid.Columns.FixedStyle.Left:
    dr["Fixed"] = "Left";
    break;
    case DevExpress.XtraGrid.Columns.FixedStyle.Right:
    dr["Fixed"] = "Right";
    break;
    case DevExpress.XtraGrid.Columns.FixedStyle.None:
    dr["Fixed"] = "None";
    break;
    default:
    dr["Fixed"] = "None";
    break;
    }

    dr["ReadOnly"] = gridView.Columns[i].OptionsColumn.ReadOnly;

    dt.Rows.Add(dr);
    }
    return dt;
    }

    public static DataTable GridFixedType()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("FixShow", typeof(string));
    dt.Columns.Add("FixName", typeof(string));

    DataRow dr1 = dt.NewRow();
    dr1[0] = "不固定";
    dr1[1] = "None";
    dt.Rows.Add(dr1);

    DataRow dr2 = dt.NewRow();
    dr2[0] = "左邊";
    dr2[1] = "Left";
    dt.Rows.Add(dr2);

    DataRow dr3 = dt.NewRow();
    dr3[0] = "右邊";
    dr3[1] = "Right";
    dt.Rows.Add(dr3);

    return dt;
    }

    public static DataTable TableSort(DataTable dt,string sort)
    {
    DataRow[] resultRows= dt.Select(null, sort);
    DataTable tableTemp = dt.Copy();
    tableTemp.Rows.Clear();
    foreach (DataRow dr in resultRows)
    {
    object[] objItem = dr.ItemArray;
    DataRow row = tableTemp.NewRow();
    row.ItemArray = objItem;
    tableTemp.Rows.Add(row);
    }
    return tableTemp;
    }

    栏目设置窗体代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace ProcessPay
    {
    public partial class FrmColumnSetup : ProcessPay.FrmBase
    {
    public FrmColumnSetup()
    {
    InitializeComponent();
    }

    public delegate void RefreshEventHandler(string strFilter);
    public event RefreshEventHandler RefreshData;

    private DataTable dt;
    private string xmlFile;

    public DataTable DT
    {
    set { dt = value; }
    get { return dt; }
    }

    public string XmlFile
    {
    set { xmlFile = value; }
    get { return xmlFile; }
    }

    private void FrmColumnSetup_Load(object sender, EventArgs e)
    {
    repositoryItemLookUpEdit1.DataSource = Common.GridFixedType();

    gridControl1.DataSource = dt;
    }

    private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
    string fieldName = e.Column.FieldName;
    int rowHandle = e.RowHandle;
    if (fieldName == "Visible")
    {
    if ((bool)gridView1.GetRowCellValue(rowHandle, fieldName))
    {
    int maxIndex = 0;
    for (int i = 0; i < gridView1.RowCount; i++)
    {
    if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > maxIndex)
    {
    maxIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString());
    }
    }
    gridView1.SetRowCellValue(rowHandle, "VisibleIndex", maxIndex + 1);
    }
    else
    {
    int visibleIndex = int.Parse(gridView1.GetRowCellValue(rowHandle, "VisibleIndex").ToString());
    for (int i = 0; i < gridView1.RowCount; i++)
    {
    if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > visibleIndex)
    {
    int tempIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) - 1;
    gridView1.SetRowCellValue(i, "VisibleIndex", tempIndex);
    }
    }
    gridView1.SetRowCellValue(rowHandle, "VisibleIndex", -1);
    }
    }
    gridView1.CloseEditor();
    }

    private void SBSave_Click(object sender, EventArgs e)
    {
    Common.DataTableToXML(dt, xmlFile);
    if (RefreshData != null)
    {
    RefreshData("");
    }
    this.Close();
    }
    }
    }

    主窗体代码:

    private void FrmMain_Load(object sender, EventArgs e)
    {
    BindDataStyle("");
    }

    private void BindDataStyle(string strFilter)
    {
    bool isExist = System.IO.File.Exists(xmlFile);
    if (isExist)
    {
    Common.GridStyleShow(gridView1, xmlFile);
    }

    BindData(strFilter);

    if (!isExist)
    {
    Common.GridStyleSave(gridView1, xmlFile);
    }
    }

    private void BindData(string strFilter)
    {
    DataSet ds = (new ProcessPay.BLL.SetNo()).GetList();
    gridControl1.DataSource = ds.Tables[0];
    }

    private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    Common.GridStyleSave(gridView1, xmlFile);
    }
    //逆ヘ砞竚
    private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
    FrmColumnSetup frm = new FrmColumnSetup();

    DataTable dt = Common.GridStyleGet(gridView1, xmlFile);
    frm.DT = dt;
    frm.XmlFile = xmlFile;
    frm.RefreshData += new FrmColumnSetup.RefreshEventHandler(BindDataStyle);
    frm.ShowDialog();
    }

    展开全文
  • WPF感悟(3)——关于向窗体动态添加控件

    万次阅读 热门讨论 2008-06-07 15:33:00
    一般情况下,控件在窗体上是写好的,只是根据需要显示与隐藏。其次,也是最重要的——我标题用的是“窗体”没有用Window,这是为什么呢?因为同样看上去是窗体,WinForm编程对应的类是Form,WPF编程对应
  • C#控件管理

    2012-03-17 08:54:01
     在项目中经常遇到要动态生成控件动态显示指定的控件或者隐藏,这就要涉及到查找控件的问题。  下面介绍下Winform中使用控件名来查找窗体或者容器中的控件的方法,代码如下:  ///  /// 通过控件名获取...
  • WPF

    2008-06-10 15:36:00
    一般情况下,控件在窗体上是写好的,只是根据需要显示与隐藏。其次,也是最重要的——我标题用的是“窗体”没有用Window,这是为什么呢?因为同样看上去是窗体,WinForm编程对应的类是Form,WPF编程对应
  • 主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、数据结构与算法、Windows窗体基础、特色窗体界面、窗体控制技术、MDI窗体和继承窗体、Windows常用控件的使用、...
  • 实例220 动态创建多个Button控件 295 11.2 TextBox控件应用 296 实例221 制作多行文本框 296 实例222 制作密码文本框 297 实例223 在TextBox控件中显示回车符 297 实例224 只允许输入数字的TextBox控件 298 实例225 ...
  • 实例220 动态创建多个Button控件 11.2 TextBox控件应用 实例221 制作多行文本框 实例222 制作密码文本框 实例223 在TextBox控件中显示回车符 实例224 只允许输入数字的TextBox控件 实例225 在TextBox控件底端...
  • 实例220 动态创建多个Button控件 11.2 TextBox控件应用 实例221 制作多行文本框 实例222 制作密码文本框 实例223 在TextBox控件中显示回车符 实例224 只允许输入数字的TextBox控件 实例225 在TextBox控件底端...
  • 实例220 动态创建多个Button控件 11.2 TextBox控件应用 实例221 制作多行文本框 实例222 制作密码文本框 实例223 在TextBox控件中显示回车符 实例224 只允许输入数字的TextBox控件 实例225 在TextBox控件底端...
  • C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 ...
  • asp.net知识库

    2015-06-18 08:45:45
    Asp.net 2.0功能体验,细节之Web控件(一) 隐藏控件 Asp.net 2.0功能体验,总体设计思想 Asp.net 2.0 WebPart使用经验点滴 革新:.NET 2.0的自定义配置文件体系初探 关于如何在ASP.NET 2.0中定制Expression ...
  • AdRotatorTest 使用广告栏控件AdRotator示例 BulletedListTest 使用项列表控件BulletedList示例 ButtonTest 使用按钮控件Button示例 CalendarTest 使用日历控件Calendar示例 CheckBoxListTest 使用...
  • HtmlInputHiddenTest 使用隐藏文本框控件HtmlInputHidden示例 HtmlInputRadioButtonTest 使用单选框控件HtmlInputRadioButton示例 HtmlInputTextTest 使用单行文本框控件HtmlInputText示例 ...

空空如也

空空如也

1 2
收藏数 32
精华内容 12
关键字:

winform动态隐藏控件