• 导入数据到GridView


     public class DataUtil
        {
            
    public DataUtil()
            {
            }

            
    #region 导入数据到GridView

            
    /// <summary>
            
    /// 导入数据到GridView
            
    /// </summary>
            
    /// <param name="fileName">源Excel文件</param>
            
    /// <param name="gridview">目标GridView</param>
            
    /// <param name="keyColIndex">导入主键列</param>
            
    /// <param name="rtnMsg">返回消息</param>
            
    /// <returns>返回没有导入的数据DataSet</returns>
            public static DataSet ExcelToGridView(string fileName, GridView gridview, int keyColIndex,string rtnMsg)
            {
                DataSet ds 
    = null ;
                
    string errMsg = string.Empty;
                
    try
                {
                    
    if ( ! fileName.EndsWith(".xls"))
                    {
                        errMsg 
    += "只能导入Excel格式的文件。";
                    }

                    ds 
    = ExcelHelper.ExcelToDS(fileName);
                    DataTable dt 
    = ds.Tables[0];
                    
    for (int i = 0; i < gridview.Rows.Count; i++)
                    {
                        
    if (gridview.Rows[i].RowType != DataControlRowType.DataRow)
                        {
                            
    continue;
                        }
                        
    string key = gridview.Rows[i].Cells[keyColIndex].Text;
                        
    if (String.IsNullOrEmpty(key))
                        {
                            
    continue;
                        }
                        
    int k = 0;
                        
    for (; k < dt.Rows.Count; k++)
                        {
                            
    if (dt.Rows[k][gridview.Columns[keyColIndex].HeaderText].ToString().Replace(" """).Replace(" """== key)
                            {
                                
    break;
                            }
                        }
                        
    if (k == dt.Rows.Count)
                        {
                            
    continue;
                        }
                        
    else
                        {
                            
    for (int j = 0; j < gridview.Columns.Count; j++)
                            {
                                
    if (gridview.Columns[j].Visible == false)
                                {
                                    
    continue;
                                }
                                
    if (gridview.Rows[i].Cells[j].Controls.Count == 0)
                                {
                                    
    continue;
                                }
                                
    for (int m = 0; m < gridview.Rows[i].Cells[j].Controls.Count; m++)
                                {
                                    
    if (gridview.Rows[i].Cells[j].Controls[m].Visible == false)
                                    {
                                        
    continue;
                                    }
                                    
    if (gridview.Rows[i].Cells[j].Controls[m] is TextBox)
                                    {
                                        
    if (!dt.Columns.Contains(gridview.Columns[j].HeaderText))
                                        {
                                            
    continue;
                                        }
                                        TextBox txt 
    = gridview.Rows[i].Cells[j].Controls[m] as TextBox;
                                        
    string temp = dt.Rows[k][gridview.Columns[j].HeaderText].ToString();
                                        
    try
                                        {
                                            
    //           int intTemp = int.Parse(temp);
                                            
    //           txt.Text = intTemp.ToString();
                                            txt.Text = temp; //Translate(temp);
                                        }
                                        
    catch (Exception)
                                        {
                                        }
                                        
    break;
                                    }
                                }
                            }
                            dt.Rows[k].Delete();
                            dt.AcceptChanges();
                        }
                    }
                }
                
    catch (Exception ex)
                {
                    errMsg 
    +="导入失败."  + ex.Message ;
                }
                rtnMsg 
    = errMsg;
                
    return ds;
            }

            
    /// <summary>
            
    /// 导入数据到GridView
            
    /// </summary>
            
    /// <param name="fileUpload">文件上传控件对象</param>
            
    /// <param name="filePath">要存放文件的服务器路径</param>
            
    /// <param name="gridview">目标GridView</param>
            
    /// <param name="keyColIndex">用于导入的主键列</param>
            
    /// <returns>返回没有导入的数据DataSet</returns>
            public static DataSet ExcelToGridView(FileUpload fileUpload, string filePath, GridView gridview, int keyColIndex, string strMsg)
            {
                
    string errMsg = string.Empty;
                DataSet ds 
    = null ;
                
    try
                {
                    
    if ( ! filePath.EndsWith("\\"))
                    {
                        filePath 
    += "\\";
                    }
                    
    string fileName = fileUpload.FileName.Replace(".xls"""+ "_" + DateTime.Now.ToString("yyyyMMddhhmmss"+ ".xls";
                    
    string fullFileName = filePath + fileName;
                    fileUpload.SaveAs(fullFileName);
                    ds 
    = ExcelToGridView(fullFileName, gridview, keyColIndex,errMsg);
                    
    if ( errMsg != string.Empty)
                    {
                        errMsg 
    += "将数据导入GridView失败." + errMsg ;
                    };
                }
                
    catch (Exception ex)
                {
                    errMsg 
    += "上传excel文件失败." ;
                }
                strMsg 
    = errMsg;
                
    return ds ;
            }

            
    #endregion

            
    /// <summary>
            
    /// 选定主键列后,用这方法将重复行用黄色背景标志
            
    /// </summary>
            
    /// <param name="gridview">目标GridView</param>
            
    /// <param name="keyColIndex">导入主键列</param>
            public static void MarkRepetitionRows(GridView gridView, int keyColIndex)
            {
                
    for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    
    for (int j = i + 1; j < gridView.Rows.Count; j++)
                    {
                        
    if (gridView.Rows[i].Cells[keyColIndex].Text == gridView.Rows[j].Cells[keyColIndex].Text)
                        {
                            gridView.Rows[i].BackColor 
    = Color.Yellow;
                            gridView.Rows[j].BackColor 
    = Color.Yellow;
                        }
                    }
                }
            }

        }
  • 相关阅读:
    (6)在树莓派上截屏的方法
    (7)树莓派读物USB摄像头
    (4)给树莓派安装中文输入法Fcitx及Google拼音输入法
    (3)使用Android手机作为树莓派的屏幕
    (2)在树莓派安装运行在Python3上的OpenCV
    相机靶面尺寸和视场角换算
    STM32F103C8T6在Arduino IDE里编程
    项目(二) esp32-cam 网页图像人脸
    开发(一) ardunio环境配置 针对esp32-cam 更多例程
    [转] Compile、Make和Build的区别
  • 原文地址:https://www.cnblogs.com/zqmingok/p/1529706.html
Copyright © 2020-2023  润新知