知识储备01:DataGridView列头字段转换为中文名称

如下图:

知识储备01:DataGridView列头字段转换为中文名称

解决代码:



        //自动生成列表,列内容对齐方式,列宽度
        private void dataGridViewAutoField() {
            string strFields = ""; //查询SQL字段合集变量
            //拼接SQL查询字段
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                strFields += "'" + dataGridView1.Columns[i].HeaderText + "',";
            }
            //去最后,
            strFields = strFields.Substring(0, strFields.Length - 1);

            //查询SQL中字段的中文名,类型,宽度
            string strSql = "";
            strSql = " SELECT fdname,chnname,fdtype,fdWidth FROM fldlist WHERE fdname IN (" + strFields.TrimEnd() + ") ";

            SqlConnection conn1 = new SqlConnection(string.Format(strSqlconnErp));
            conn1.Open();
            SqlDataAdapter da1 = new SqlDataAdapter(strSql, conn1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1, "table"); 

            //与dataGridView列表头进行匹配
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                string szColumnName = dataGridView1.Columns[i].HeaderText; //列名 
                int intFdwidth = 0;
                //没找到时,保留原英文名
                if (ds1.Tables["table"].Rows.Count == 0)
                {
                    dataGridView1.Columns[i].HeaderText = szColumnName;
                }
                else
                {
                    for (int i2 = 0; i2 < ds1.Tables["table"].Rows.Count; i2++)
                    {
                        string strFdname = "";
                        string strChnname = "";
                        string strFdtype = "";
                        strFdname = ds1.Tables["table"].Rows[i2][0].ToString().TrimEnd();
                        strChnname = ds1.Tables["table"].Rows[i2][1].ToString().TrimEnd();
                        strFdtype = ds1.Tables["table"].Rows[i2][2].ToString().TrimEnd();
                        intFdwidth = Convert.ToInt32(ds1.Tables["table"].Rows[i2][3].ToString());

                        if (szColumnName == strFdname)
                        {
                            //改列头
                            dataGridView1.Columns[i].HeaderText = strChnname;
                            switch (strFdtype)
                            {
                                case "字符":
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                                    break;
                                case "字串":
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                                    break;
                                case "文本":
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                                    break;
                                case "整数":
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                                    break;
                                case "实数":
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                                    break;
                                default:
                                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                                    break;
                            }
                            dataGridView1.Columns[i].Width = intFdwidth;

                        }else{
                            dataGridView1.Columns[i].HeaderText = szColumnName;
                        }
                        
                    }      
                }
                conn1.Close(); 
           } 
        }

 

上一篇:C# DataGridView中指定的单元格不能编辑


下一篇:c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据