PDA

Click to See Complete Forum and Search --> : [2.0] Problems with counter


martins
Nov 14th, 2006, 03:35 AM
Hi! Heres the code Im having troubles with:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Bonuss
{
public partial class frmList : Form
{
public frmList()
{
InitializeComponent();
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}

void frmList__myElapsed(object sender, long Ticks, DataGridViewRow row)
{
if (dataGridView1.Rows[row.Index].Tag != null)
{
dataGridView1[7, row.Index].Value = Ticks.ToString();
}
}

//void frmList__myElapsed(object sender, long Ticks, int rowIndex)
//{
// dataGridView1[7, rowIndex].Value = Ticks.ToString();
//}

private void frmList_Load(object sender, EventArgs e)
{
dataSet1.ReadXml("dati/dati.xml");
dataGridView1.DataSource = dataSet1.Tables[0];
AddButtonColumn();

ReloadData();

}

private void ReloadData()
{
for (int i = 0; i <= dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1.Rows[i].Tag == null)
{
dataGridView1.Rows[i].Tag = new rowTag(dataGridView1.Rows[i]);
((rowTag)dataGridView1.Rows[i].Tag)._myElapsed += new rowTag.myElapsed(frmList__myElapsed);
}
else
{
int err = 0;
}
}
}

DataGridViewButtonColumn buttons;

private void AddButtonColumn()
{
buttons = new DataGridViewButtonColumn();
{
buttons.HeaderText = "Opcijas";
buttons.Text = "--";
buttons.UseColumnTextForButtonValue = false;
buttons.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
buttons.FlatStyle = FlatStyle.Standard;
buttons.CellTemplate.Style.BackColor = Color.Honeydew;
buttons.DisplayIndex = 8;
buttons.DefaultCellStyle.NullValue = "<+/->";
}

dataGridView1.Columns.Add(buttons);
}

public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

if (dataGridView1.Columns[e.ColumnIndex].HeaderText=="Opcijas" )
{

try
{
rowTag currentRowTag = ((rowTag)dataGridView1.Rows[e.RowIndex].Tag);
if (!currentRowTag.isRunning)
{
currentRowTag.Start();
}
else
{
currentRowTag.Finish();
}
}
catch (Exception)
{
int a = 0;
}
try
{
if (dataGridView1[7, e.RowIndex].Value == "X")
{
if (e.ColumnIndex == buttons.Index)
{
//MessageBox.Show("" + dataGridView1[7, e.RowIndex].Value);
dataGridView1[7, e.RowIndex].Value = 0;
}
}
else if (dataGridView1[7, e.RowIndex].Value != "X" )
{
if (e.ColumnIndex == buttons.Index)
{
dataGridView1[7, e.RowIndex].Value = "X";

//MessageBox.Show("" + dataGridView1[5, e.RowIndex].Value);

float ft_Counter = 0;

try
{
ft_Counter = Convert.ToSingle(dataGridView1[9, e.RowIndex].Value);
}
catch (Exception)
{
ft_Counter = 0;
}

dataGridView1[9, e.RowIndex].Value = ft_Counter + 1;
}

}
}
catch (Exception aaa)
{
MessageBox.Show("Kļūda! " + aaa);
}
}
}

private void button1_Click(object sender, EventArgs e)
{
dataSet1.WriteXml("dati/dati.xml");

}

}
}


problem is - when I press the button in grid (see - private void AddButtonColumn()) for the first time, counter adds 1 to row nine, when I press it repeatedly it works only on every second click. I want it to work on every second click at all times! how can I fix that? as you can see - on a button click timer is trigered. Counters job is to count how many times timer has been started. But now - for the first time it counts when timer starts and ends, but later on it does the job right