Small operations

How to add a database into DataGridView?

Data query using DataTable storage.

For example the DataTable dt; dt object data exists

DataGirdView.DataSource=dt;

This put the data there is a DataGridView.

 

Data DataPropertyName need to column names in the database of a column in the Columns in the same complete the binding.

For example, do not show the class, you need to change the name to Shift (that is, the column names in the database).

When displayed, do not show how the order Edit Columns in?

 DataGirdView.AutoGenerateColumns = false;

Sequence can be made properly.

DataGridView DataTable is not only acceptable but also can receive it accepts a List object.

Increase in number.

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

 

 The data in a column in the DataTable modified to want

For example: in the database is displayed to show empty EMPTY similar.

 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 will be taken over the entire data table, the data can actually use is not necessarily the entire table all the data.

Use List may receive data to be used.

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

Class is used to define a target as a received data, and the class contained in the received required properties. To add an object to a List. GridTable accepted List.

List data may be selectively receiving portion by, in addition to the data in the List will be screened for display of the information satisfies the condition portion.

Screening List Information

 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

Where a screening method using List, comprising screening according to the condition of the drop-down box, may be a plurality of single.

List Gets a value for each row

For example to obtain the status of each line, it will change.

            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 to quickly generate property

prop double-click Tab

Select a value of

Listed as

string a;

string b;

Select the string can be selected to join ALT easy to modify.

String Date transformed into the DateTime then into String-> format conversion and to increase the number of days

String strSftDate = " 20,191,016 "; 
the DateTime DATE = DateTime.ParseExact (strSftDate, " yyyyMMdd " , System.Globalization.CultureInfo.CurrentCulture); 
strSftDate = date.AddDays ( . 1 ) .ToString ( " yyyyMMdd " );
 // number of days increased and returns sting because DateTime format can not afford to go every minute
View Code

 C # winform timer controls (see CSDN author una_ting 's blog)

timer_MainForm.Enable = true;

timer_MainForm.Interval = 500; // timer interval

timer_MainForm.Start (); // timer starts operation

Set the timer property

Timing of realization of behavior can be achieved in the Tick event.

  Private  void timer_MainForm_Tick ( Object SENDER, EventArgs E) 
 { 

     // concrete implementation 

     //
 
}
View Code

 

The color code 10 hex

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

DataGridView color of each row alternates

// background color setting table
grdData.RowsDefaultCellStyle.BackColor = Color.White;
// set the background color of alternate rows
grdData.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua;

 

Guess you like

Origin www.cnblogs.com/cdjbolg/p/11677095.html