Saturday, October 26, 2013

Winforms populate dynamic combobox

private void GenerateAuto()
        {
            try
            {

                dGVQCEngg.Rows.Clear();
                dGVQCEngg.Columns.Clear();
                UserService.Users objUserService = new UserService.Users();
                DataTable DTUsers = new DataTable();
                if (strUserGroup == "Group 1")
                {
                    //Method for populate dropdownlist values
                    DTUsers = objUserService.GetAllUsersDashBoardForGroup1();
                }
                else if (strUserGroup == "Group 2")
                {
                    //Method for populate dropdownlist values
                    DTUsers = objUserService.GetAllUsersDashBoardForGroup2();
                }

                DataGridViewRow row = new DataGridViewRow();
                dGVQCEngg.AllowUserToAddRows = true;



                DataGridViewComboBoxColumn cboPartsColumn = new DataGridViewComboBoxColumn();
                cboPartsColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
                cboPartsColumn.DataSource = DTUsers;
                cboPartsColumn.HeaderText = "Engg Name";
                cboPartsColumn.DisplayMember = "UserName";
                cboPartsColumn.ValueMember = "Id";
                cboPartsColumn.DataPropertyName = "Id";
                cboPartsColumn.Width = 150;
                cboPartsColumn.AutoComplete = true;
                dGVQCEngg.Columns.Add(cboPartsColumn);

                MastersService.Masters ObjMaster = new MastersService.Masters();
                DataTable DT_Processes = ObjMaster.GetCodeValuesByParentCode("Process");

                DataGridViewComboBoxColumn cmbStatus = new DataGridViewComboBoxColumn();
                cmbStatus.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
                cmbStatus.DataSource = DT_Processes;
                cmbStatus.HeaderText = "Section";
                cmbStatus.DisplayMember = "ShortDesc";
                cmbStatus.ValueMember = "Id";
                cmbStatus.DataPropertyName = "Id";
                cmbStatus.Width = 150;
                cmbStatus.AutoComplete = true;
                dGVQCEngg.Columns.Add(cmbStatus);

                CalendarColumn col = new CalendarColumn();
                col.HeaderText = "Date";
                dGVQCEngg.Columns.Add(col);
                dGVQCEngg.Rows[0].Cells[2].Value = DateTime.Now;

                DataGridViewButtonColumn btnAdd = new DataGridViewButtonColumn();
                btnAdd.HeaderText = "Add New";
                btnAdd.Text = "Add New";
                btnAdd.ToolTipText = "Add New Row";
                btnAdd.Name = "Add";
                btnAdd.UseColumnTextForButtonValue = true;
                dGVQCEngg.Columns.Add(btnAdd);

                DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
                btnDelete.HeaderText = "Remove";
                btnDelete.Text = "Remove";
                btnDelete.ToolTipText = "Remove Row";
                btnDelete.Name = "Delete";
                btnDelete.UseColumnTextForButtonValue = true;
                dGVQCEngg.Columns.Add(btnDelete);

                dGVQCEngg.Rows.Add();
                dGVQCEngg.Rows[0].Cells[2].Value = DateTime.Now;
                dGVQCEngg.AllowUserToAddRows = false;
                Common.FormatDataGrid(dGVQCEngg);


            }
            catch (Exception)
            {

                throw;
            }
        }
       
       public static void FormatDataGrid(DataGridView dgv)
        {

            dgv.EnableHeadersVisualStyles = false;
            //dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            dgv.AllowUserToOrderColumns = true;
            dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgv.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
            dgv.RowHeadersVisible = false;
            dgv.BorderStyle = BorderStyle.FixedSingle;

            DataTable DT_GridSettings = new DataTable();
            if (AppDomain.CurrentDomain.GetData("GridColors") != null)
            {

                DT_GridSettings = (DataTable)AppDomain.CurrentDomain.GetData("GridColors");
                string strFontSettings = DT_GridSettings.Rows[0]["Fonts"].ToString();
                Color RowBackColor = Color.White;
                Color RowForeColor = Color.Black;
                Color AlternateBackColor = Color.White;
                Color AlternateForeColor = Color.Black;
                Color SelectedBackColor = Color.LightGray;
                Color SelectedForeColor = Color.Black;
                Color HeaderBackColor = Color.LightGray;
                Color HeaderForeColor = Color.Black;

                RowBackColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["RowBackColor"].ToString()));
                RowForeColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["RowForeColor"].ToString()));
                AlternateBackColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["AltRowBackColor"].ToString()));
                AlternateForeColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["AltRowForeColor"].ToString()));
                SelectedBackColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["SelRowBackColor"].ToString()));
                SelectedForeColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["SelRowForeColor"].ToString()));
                HeaderBackColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["ColumnHeaderBackColor"].ToString()));
                HeaderForeColor = Color.FromArgb(int.Parse(DT_GridSettings.Rows[0]["ColumnHeaderForeColor"].ToString()));

                dgv.RowsDefaultCellStyle.BackColor = RowBackColor;
                dgv.RowsDefaultCellStyle.ForeColor = RowForeColor;
                dgv.AlternatingRowsDefaultCellStyle.BackColor = AlternateBackColor;
                dgv.AlternatingRowsDefaultCellStyle.ForeColor = AlternateForeColor;
                dgv.RowsDefaultCellStyle.SelectionBackColor = SelectedBackColor;
                dgv.RowsDefaultCellStyle.SelectionForeColor = SelectedForeColor;
                dgv.ColumnHeadersDefaultCellStyle.BackColor = HeaderBackColor;
                dgv.ColumnHeadersDefaultCellStyle.ForeColor = HeaderForeColor;
                if (strFontSettings != null && strFontSettings != string.Empty)
                {
                    string strFontName = string.Empty;
                    float flFontSize = (float)0.0;
                    bool blBold = false;
                    bool blItalic = false;

                    string[] Fonts = strFontSettings.Split(';');
                    foreach (string FontSetting in Fonts)
                    {
                        if (FontSetting != string.Empty)
                        {
                            string[] NameValue = FontSetting.Split(',');
                            string strName = NameValue[0];
                            string strValue = NameValue[1];
                            if (strName != string.Empty)
                            {
                                if (strName.ToLower() == "name")
                                {
                                    strFontName = strValue;
                                }
                                else if (strName.ToLower() == "size")
                                {
                                    flFontSize = (float)Convert.ToDecimal(strValue);
                                }
                                else if (strName.ToLower() == "bold")
                                {
                                    blBold = Convert.ToBoolean(strValue);
                                }
                                else if (strName.ToLower() == "italic")
                                {
                                    blItalic = Convert.ToBoolean(strValue);
                                }
                            }
                        }
                    }


                    if (strFontName != string.Empty && flFontSize != (float)0.0)
                    {
                        Font GridFonts = null;
                        if (blBold && blItalic)
                        {
                            GridFonts = new Font(strFontName, flFontSize, FontStyle.Bold | FontStyle.Italic);
                        }
                        else if (blBold)
                        {
                            GridFonts = new Font(strFontName, flFontSize, FontStyle.Bold);
                        }
                        else if (blItalic)
                        {
                            GridFonts = new Font(strFontName, flFontSize, FontStyle.Italic);
                        }
                        else
                        {
                            GridFonts = new Font(strFontName, flFontSize, FontStyle.Regular);
                        }
                        dgv.Font = GridFonts;

                    }
                }

            }
            else
            {
                dgv.DefaultCellStyle.SelectionBackColor = Color.White;
                dgv.DefaultCellStyle.SelectionBackColor = Color.Black;
                dgv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightSkyBlue;
                dgv.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.DodgerBlue;
                dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
                dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                dgv.RowsDefaultCellStyle.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                dgv.RowsDefaultCellStyle.BackColor = Color.LightBlue;
                dgv.RowsDefaultCellStyle.ForeColor = Color.Black;
                dgv.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.DodgerBlue;
                dgv.RowsDefaultCellStyle.SelectionForeColor = Color.Black;
            }
            //Clipboard.SetText(Convert.ToString(dgv.Rows[RowIndex].Cells[ColumnIndex].Value));
        }



// For Save
int intlength = dGVQCEngg.Rows.Count;
                        if (intlength > 0)
                        {
                            for (int i = 0; i < intlength; i++)
                            {
                                long lngOperatorId = Convert.ToInt64(dGVQCEngg.Rows[i].Cells[0].Value);
                                string strOperatorName = Convert.ToString(dGVQCEngg.Rows[i].Cells[0].FormattedValue);
                                long lngProcessId = Convert.ToInt64(dGVQCEngg.Rows[i].Cells[1].Value);
                                string strProcessname = Convert.ToString(dGVQCEngg.Rows[i].Cells[1].FormattedValue);
                                DateTime CntInDate = Convert.ToDateTime(dGVQCEngg.Rows[i].Cells[2].Value);
                                strQualityDetails += "QCAutoId_Id" + "," + lngOperatorId + ",''" + strOperatorName + "''," + lngProcessId + ",''" + strProcessname + "'',''" + CntInDate + "''|";
                            }
                        }