小操作

如何将数据库添加进DataGridView?

将查询的数据用DataTable存储。

例如 DataTable dt;数据存在dt对象中

DataGirdView.DataSource=dt;

这样就把数据存在了DataGridView.

 

在Columns中 需要将Data一栏中的DataPropertyName与数据库中的列名相同完成绑定。

例如 显示班别,需要将名字改为Shift(即数据库中的列名).

显示的时候,没有按照Edit Columns中的顺序显示怎么做?

 DataGirdView.AutoGenerateColumns = false;

可以使顺序正常。

DataGridView不仅可以接受DataTable 也可以接收List 它接受的是一个对象.

增加序号。

int coun = grdData.RowCount;
for (int i = 0; i < coun - 1; i++)
{
grdData.Rows[i].Cells["RowID"].Value = i + 1;//引号中为列名
}
View Code

 将DataTable中某列的数据修改成想要的

例如:在数据库中显示的是EMPTY 要显示为空类似的.

 for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[j]["Ref_Status"].ToString().Equals(""))
                    {
                        strRefStatus = "EMPTY";
                    }
                    else
                    {
                        strRefStatus = dt.Rows[j]["Ref_Status"].ToString();
                    }
                    if (strRefStatus != "FULL")
                    {
                        strStatus = "";
                        if (strRefStatus == "EMPTY")
                        {
                            strStatus = "备料";
                        }
                        if (strStatus == "PREPARE")
                        {
                            strStatus = "备料中";
                        }
                        if (strRefStatus == "WAIT")
                        {
                            strStatus = "等待";
                        }
                    }
                }
                grdData.Rows[j].Cells["Status"].Value = strStatus;
            }
View Code

DataTable会将整个数据表接收过来,可真正使用的数据未必是整个数据表所有的数据。

使用List可以接收需要使用的数据.

public class Data_Model
    {
        public string Shift { get; set; }
        public string Line { get; set; }
        public string SO { get; set; }
        public string RefNo { get; set; }
        public string PartNo { get; set; }
        public string SoQty { get; set; }
        public string chgSoQty { get; set; }
        public string PlanStartTime { get; set; }
        public string WaitMin { get; set; }
        public string Remark { get; set; }
        public string StartTime{ get; set; }
      
    }
View Code
 List<Data_Model> list = new List<Data_Model>();
            for (int m = 0; m < dt.Rows.Count; m++)
            {
                Data_Model model = new Data_Model();
                model.Shift = dt.Rows[m]["Shift"].ToString();
                model.Line = dt.Rows[m]["Line"].ToString();
                model.SO = dt.Rows[m]["So"].ToString();
                model.RefNo = dt.Rows[m]["RefNo"].ToString();
                model.SoQty = dt.Rows[m]["SoQty"].ToString();
                model.chgSoQty = dt.Rows[m]["chgSoQty"].ToString();
                model.StartTime = dt.Rows[m]["StartTime"].ToString();
                model.PlanStartTime = dt.Rows[m]["PlanStartTime"].ToString();
                model.WaitMin = dt.Rows[m]["WaitMin"].ToString();
                model.Remark = dt.Rows[m]["Remark"].ToString();
                list.Add(model);
            }
View Code

定义一个类用来作为接收数据的对象,并且类中含有需要接收的属性。将对象添加到List中。GridTable接受List.

通过List可以选择性的接收部分数据,此外List中的数据也会进行筛选,显示部分满足 条件的信息。

筛选List信息

 if (cboLine.Text != "ALL" && cboSO.Text != "ALL" && cboStatus.Text == "ALL")
            {
                list = list.Where(a => a.Line.Contains(cboLine.Text) && a.SO.Contains(cboSO.Text)).ToList();
            }
View Code

筛选List使用的是Where方法,根据下拉框中的筛选包含条件,可多个也可单个。

List获取每行的某个值

例如获取每行的状态,将其改变。

            int coun = grdData.RowCount;
            for (int i = 0; i < coun; i++)
            {
                grdData.Rows[i].Cells["RowID"].Value = i + 1;
            }
            for (int j = 0; j < coun; j++)
            {
                if (grdData.RowCount > 0)
                {
                    if (list[j].Status.ToString().Equals(""))
                    {
                        strRefStatus = "EMPTY";
                    }
                    else
                    {
                        strRefStatus = list[j].Status.ToString();
                    }
                    if (strRefStatus != "FULL")
                    {
                        strStatus = "";
                        if (strRefStatus == "EMPTY")
                        {
                            strStatus = "备料";
                        }
                        if (strStatus == "PREPARE")
                        {
                            strStatus = "备料中";
                        }
                        if (strRefStatus == "WAIT")
                        {
                            strStatus = "等待";
                        }
                    }
                }
                grdData.Rows[j].Cells["Status"].Value = strStatus;
            }
View Code

快速生成Get Set属性

prop 按两下Tab

选中一列的值

列如

string a;

string b;

选中string 加入ALT便可选中 方便修改。

将String日期转成DateTime再转成String->增加天数和格式转换

string strSftDate=“20191016”;
DateTime date=DateTime.ParseExact(strSftDate,"yyyyMMdd",System.Globalization.CultureInfo.CurrentCulture);
strSftDate = date.AddDays(1).ToString("yyyyMMdd");
//完成天数增加并且返回sting 因为DateTime格式时分秒去不掉
View Code

 C# winform 定时器控件(看CSDN 作者una_ting的博客)

timer_MainForm.Enable = true;

timer_MainForm.Interval = 500;  //定时器时间间隔

timer_MainForm.Start();   //定时器开始工作

设置定时器属性

定时实现的行为可在Tick事件中实现。

  private void timer_MainForm_Tick(object sender, EventArgs e)
 {

     //具体实现的方法

     //

}
View Code

根据10进制颜色代码

backcolor=ColorTranslator.FromHtml("#" + Color);

DataGridView 每行的颜色交替显示

//设置表格背景色
grdData.RowsDefaultCellStyle.BackColor = Color.White;
//设置交替行的背景色
grdData.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua;

猜你喜欢

转载自www.cnblogs.com/cdjbolg/p/11677095.html