Ejemplo n.º 1
0
        private void TxtBuscar_TextChanged(object sender, EventArgs e)
        {
            Dictionary <char, int> valores = new Dictionary <char, int>
            {
                { 'A', 4 },
                { 'B', 3 },
                { 'C', 2 },
                { 'D', 1 },
                { 'F', 0 }
            };

            LBIndice.Text = "0";
            LBHonor.Text  = "Ninguno";
            DataBaseControl DBControl = new DataBaseControl(DataBaseControl.cPath, "ProjectDataBase.mdf");
            BindingSource   AlmSource = new BindingSource();

            string[] parameters = { "@word" };
            string[] value      = { TxtBuscar.Text };
            AlmSource.DataSource           = DBControl.ObtenerTabla("Select [Materia],[Calificacion],[CalificacionLiteral] from [Registro] where [Alumno] =@word", parameters, value);
            DGVAlumnos.DataSource          = AlmSource;
            DGVAlumnos.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DGVAlumnos.ReadOnly            = true;
            string[] para;
            string[] val;
            try
            {
                float suma = 0, total = 0;
                for (int i = 0; i < DGVAlumnos.Rows.Count; i++)
                {
                    para = new string[] { "@id" };
                    val  = new string[] { DGVAlumnos.Rows[i].Cells[0].Value.ToString() };
                    int credito = int.Parse(DBControl.BuscarElemento("Select [Credito] from [Materias] where [Clave]=@id", para, val));
                    total += credito;
                    string literal = (DGVAlumnos.Rows[i].Cells[2].Value.ToString().ToUpper());
                    string test    = "" + literal;
                    suma += valores[literal[0]] * credito;
                }
                LBIndice.Text = (suma / total).ToString();
                double indice = double.Parse(LBIndice.Text);
                if (indice > 3.80 && indice < 4.0)
                {
                    LBHonor.Text = "Summa Cum Laude";
                }
                if (indice > 3.50 && indice < 3.79)
                {
                    LBHonor.Text = "Magna Cum Laude";
                }
                if (indice > 3.20 && indice < 3.49)
                {
                    LBHonor.Text = "Cum Laude";
                }
                if (indice < 3.19)
                {
                    LBHonor.Text = "Sin honor";
                }
                SqlConnection con = new SqlConnection();
                using (SqlCommand cmd = new SqlCommand("Update [Alumnos] set [Indice]=@nota where [ID]=@alumno", con))
                {
                    cmd.Parameters.AddWithValue("@alumno", TxtBuscar.Text);
                    cmd.Parameters.AddWithValue("@nota", float.Parse(LBIndice.Text));
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex) { /*MessageBox.Show(ex.Message);*/ }
        }
Ejemplo n.º 2
0
        private void DGVREgistro_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (int.TryParse(DGVREgistro.CurrentRow.Cells[2].Value.ToString(), out int grade))
                {
                    if (grade > 100 || grade < 0)
                    {
                        MessageBox.Show("Imposible agregar una calificación con este valor");
                        DGVREgistro.CurrentRow.Cells[2].Value = "0";
                    }
                    if (grade >= 90)
                    {
                        DGVREgistro.CurrentRow.Cells[3].Value = "A";
                    }
                    else if (grade >= 80)
                    {
                        DGVREgistro.CurrentRow.Cells[3].Value = "B";
                    }
                    else if (grade >= 70)
                    {
                        DGVREgistro.CurrentRow.Cells[3].Value = "C";
                    }
                    else if (grade >= 60)
                    {
                        DGVREgistro.CurrentRow.Cells[3].Value = "D";
                    }
                    else if (grade < 60)
                    {
                        DGVREgistro.CurrentRow.Cells[3].Value = "F";
                    }
                    string cPath = System.IO.Directory.GetCurrentDirectory() + @"\";
                    //public static string cPath = Path.GetFullPath(@"..\..\");
                    string _location = "", _DataBaseName = "ProjectDataBase.mdf";

                    _location = cPath + @"ApplicationData\" + _DataBaseName;
                    String path = @"Data source = (localdb)\v11.0 ; AttachDbFilename=" + _location + "; MultipleActiveResultSets=True; Integrated Security=SSPI";
                    con.ConnectionString = path;
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand("Update [Registro] set [Alumno]=@alumno, [Materia] = @materia,[Calificacion]=@cal,[CalificacionLiteral] = @calLit where [Alumno]=@alumno and [Materia]=@materia", con))
                    {
                        cmd.Parameters.AddWithValue("@alumno", DGVREgistro.CurrentRow.Cells[0].Value);
                        cmd.Parameters.AddWithValue("@materia", DGVREgistro.CurrentRow.Cells[1].Value);
                        cmd.Parameters.AddWithValue("@cal", DGVREgistro.CurrentRow.Cells[2].Value);
                        cmd.Parameters.AddWithValue("@calLit", DGVREgistro.CurrentRow.Cells[3].Value);
                        cmd.ExecuteNonQuery();
                    }
                    float[]          suma  = new float[200];
                    float[]          total = new float[200];
                    string[]         para;
                    string[]         val;
                    HashSet <string> IDS = new HashSet <string>();
                    for (int i = 0; i < DGVREgistro.Rows.Count; i++)
                    {
                        IDS.Add(DGVREgistro.Rows[i].Cells[0].Value.ToString());
                    }
                    Dictionary <string, int> Evaluar = new Dictionary <string, int>();
                    int a = 0;
                    foreach (string item in IDS)
                    {
                        a++;
                        Evaluar.Add(item, a);
                    }
                    Dictionary <char, int> valores = new Dictionary <char, int>
                    {
                        { 'A', 4 },
                        { 'B', 3 },
                        { 'C', 2 },
                        { 'D', 1 },
                        { 'F', 0 }
                    };
                    DataBaseControl DBControl = new DataBaseControl(DataBaseControl.cPath, "ProjectDataBase.mdf");
                    string          LID       = DGVREgistro.Rows[0].Cells[0].Value.ToString();
                    suma[0]  = 0;
                    total[0] = 0;
                    for (int i = 0; i < DGVREgistro.Rows.Count; i++)
                    {
                        string CID = DGVREgistro.Rows[i].Cells[0].Value.ToString();
                        if (LID != CID)
                        {
                            LID = DGVREgistro.Rows[i].Cells[0].Value.ToString();
                            total[Evaluar[CID]] = 0;
                            suma[Evaluar[CID]]  = 0;
                        }
                        para = new string[] { "@id", "alum" };
                        val  = new string[] { DGVREgistro.Rows[i].Cells[1].Value.ToString() };
                        int credito = int.Parse(DBControl.BuscarElemento("Select [Credito] from [Materias] where [Clave]=@id", para, val));
                        total[Evaluar[CID]] += credito;
                        string literal;
                        literal = (DGVREgistro.Rows[i].Cells[3].Value.ToString().ToUpper());
                        if (literal.Length < 1)
                        {
                            literal = "F";
                        }
                        string test = "" + literal;
                        suma[Evaluar[CID]] += valores[literal[0]] * credito;
                    }
                    LID = DGVREgistro.Rows[0].Cells[0].Value.ToString();
                    for (int i = 0; i < DGVREgistro.Rows.Count; i++)
                    {
                        string CID = DGVREgistro.Rows[i].Cells[0].Value.ToString();
                        if (LID != DGVREgistro.Rows[i].Cells[0].Value.ToString())
                        {
                            LID = DGVREgistro.Rows[i].Cells[0].Value.ToString();
                        }
                        float notea = (suma[Evaluar[CID]] / total[Evaluar[CID]]);
                        para = new string[] { "@algo" };
                        val  = new string[] { DGVREgistro.Rows[i].Cells[0].Value.ToString() };
                        string ID = DBControl.BuscarElemento("Select [ID] from Alumnos where [ID]=@algo", para, val);
                        using (SqlCommand cmd = new SqlCommand("Update [Alumnos] set [Indice]=@nota where [ID]=@alumno", con))
                        {
                            cmd.Parameters.AddWithValue("@alumno", ID);
                            cmd.Parameters.AddWithValue("@nota", Math.Round(notea, 2));
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Por favor, digite un número");
                    DGVREgistro.CurrentRow.Cells[2].Value = null;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            con.Close();
        }