Autor Beitrag
okrim
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: Sa 19.04.14 22:42 
Hallo an alle,

habe ein dataGridView wo ich manuell eine Zahl eingebe, jetzt hätte ich gerne das beim Klick auf ein Botton die Zahl mit zwei Dezimalstellen angezeigt wird.

Beispiel:
Eingabe --> 4, nach Button Klick, soll 4,00 angezeigt werden

Habe schon folgendes ausprobiert aber leider ohne Erfolg.
ausblenden C#-Quelltext
1:
2:
3:
4:
        private void button3_Click(object sender, EventArgs e)
        {
             dataGridView1.Columns["grid_stunden"].DefaultCellStyle.Format = "N2";
        }



Danke vorweg für eure Antworten
Gruß Mirko
okrim Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: So 20.04.14 09:54 
habs hin bekommen, vieleicht nicht schön aber es funktioniert :D

ausblenden C#-Quelltext
1:
2:
3:
            dataGridView1.Columns["Column1"].DefaultCellStyle.Format = "N2";
            double std_neu = Convert.ToDouble(dataGridView1.Rows[0].Cells["Column1"].Value);
            dataGridView1.Rows[0].Cells["Column1"].Value = std_neu;


Trotzdem Danke an alle.
okrim Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: So 20.04.14 11:26 
jetzt hätte ich doch noch ne Frage und zwar hätte ich gerne das das ganze auch funkzioniert wenn ich die Enter-Taste betätige

habe jetzt folgenden code
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns["Column1"].DefaultCellStyle.Format = "N2";
            double std_neu = Convert.ToDouble(dataGridView1.Rows[0].Cells["Column1"].Value);
            dataGridView1.Rows[0].Cells["Column1"].Value = std_neu;
        }


wenn ich es mit dataGridView1_KeyDown versuche geht es ja nur wenn ich nicht im Edit-Mode bin, gibt es auch eine Möglichkeit wenn ich im Edit-Mode bin?
Yankyy02
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: So 20.04.14 13:09 
Hallo okrim,

am einfachsten wäre es die Anzeige zu aktualisieren sobald CellEndEdit aufgerufen wird denn nicht jeder verläßt die Zelle mit der Enter Taste. Ich würde daß an deiner Stelle so lösen wobei natürlich geprüft werden muss ob sich die "Zelle" in deiner Spalte befindet.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
      double Number = double.Parse(dataGridView1.CurrentCell.Value.ToString());
      dataGridView1.CurrentCell.Value = string.Format("{0:N}", Number);
}


MfG

_________________
the quiter you become, the more you are able to hear

Für diesen Beitrag haben gedankt: okrim
okrim Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: So 20.04.14 13:56 
Danke Yankyy02,

habe es geändert sieht besser aus :D

nur mein Problem mit der Enter-Taste habe ich immer noch, ich beschreibe mal meine Funktion bis jetzt.
ich habe ein dataGridView das ich aus eine Access-Datenbank fülle, mit Kunde, Tätigkeit und Stunden, jetzt habe ich einen Button Bearbeiten eingefügt wo ich nur die Stunden ändern kann.
Wenn ich also auf Bearbeiten gehe dann ändert sich die davor Markierte Zelle in der Spalte Stunden in ReadOnly false
Und ich gebe einen neuen Wert ein, danach auf den Button Speichern und alles ist gut.

Soweit funktioniert das auch alles wunderbar nur hätte ich gerne das ich auch einfach auf Enter drücken kann wenn ich den Wert geändert habe und nicht immer mit der Maus auf Speichern muss.
Yankyy02
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: So 20.04.14 19:21 
Hallo,

folgender Code sollte dir weiterhelfen.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            var key = new KeyEventArgs(keyData);

            PressedKey(this, key);

            return base.ProcessCmdKey(ref msg, keyData);
        }


        private void PressedKey(object sender, KeyEventArgs key)
        {
            switch (key.KeyCode)
            {
                case Keys.Enter:
                    dataGridView1.EndEdit();
                    double Number = double.Parse(dataGridView1.SelectedCells[0].Value.ToString());
                    dataGridView1.SelectedCells[0].Value = string.Format("{0:N}", Number);
                    
                    break;
            }
        }


Anbei der Link zur Methode msdn.microsoft.com/e...y%28v=vs.110%29.aspx

MfG

_________________
the quiter you become, the more you are able to hear

Für diesen Beitrag haben gedankt: okrim
okrim Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

Win 7
C# (VS 2010 Express)
BeitragVerfasst: So 20.04.14 20:37 
Hallo Yankyy02,

tausend Dank, das klappt wunderbar.

Gruß Mirko