DataGridView question ... i'm hopeless

G

Guy

I'm trying to automatically, using a timer, refresh a DataGridView (see
attached code), but i don't succeed to display all rows correctly. I used
both FirstDisplayedScrollingRowIndex as well CurrentCell but doesn't work
(seeing all rows correctly). The delegate is only meant to call the MainLoop
method after all rows are displayed. Please can someone give me some help on
this, especially on the DisplayRows method? Many thanks.

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

namespace DigitalTournamentTracker
{
public partial class frmMain : Form
{
DataTable dt = null;
public delegate void SimpleDelegate();
int nextView = 0;
int counter = 1;

public frmMain()
{
InitializeComponent();
}

private void InitializeDataGridView()
{
DataGridViewRow row = this.dataGridView1.RowTemplate;
row.Height = 75;
//row.MinimumHeight = 35;

dataGridView1.ReadOnly = true;
dataGridView1.Font = new Font("Arial", 18, FontStyle.Bold);
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.None;
//dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.DisplayedCells;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
DataGridViewCellStyle dgvcsDef = new DataGridViewCellStyle();
dgvcsDef.WrapMode = DataGridViewTriState.True;
dgvcsDef.ForeColor = Color.Black;
dgvcsDef.BackColor = Color.MintCream;
dgvcsDef.Alignment = DataGridViewContentAlignment.TopCenter;
dataGridView1.DefaultCellStyle = dgvcsDef;

DataGridViewCellStyle dgvcsAlt = new DataGridViewCellStyle();
dgvcsAlt.WrapMode = DataGridViewTriState.True;
dgvcsAlt.ForeColor = Color.Black;
dgvcsAlt.BackColor = Color.LightGray;
dgvcsAlt.Alignment = DataGridViewContentAlignment.TopCenter;
dataGridView1.AlternatingRowsDefaultCellStyle = dgvcsAlt;
dataGridView1.RowHeadersVisible = false;

}

private void MainLoop()
{
MessageBox.Show("Enter MainLoop" + " " + nextView.ToString());
switch (nextView)
{
case 0:
// Display dag wedstrijdplanning
nextView = 1;
MessageBox.Show("Enter case 0");
SqlCommand command = new
SqlCommand("GetDagWedstrijdplanning");
command.Parameters.Add("@xyz", SqlDbType.DateTime).Value
= "2007-07-24";
GetRows(command);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].HeaderText = "Uur";
dataGridView1.Columns[1].HeaderText = "xxx";
dataGridView1.Columns[2].HeaderText = "yyy";
dataGridView1.Columns[3].HeaderText = "zzz";
dataGridView1.Columns[4].HeaderText = "ttt";
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[1].Width = 513;
dataGridView1.Columns[2].Width = 514;
dataGridView1.Columns[3].Width = 100;
dataGridView1.Columns[4].Width = 100;
//dataGridView1.FirstDisplayedScrollingRowIndex = 0;
timer1.Enabled = true;
break;
case 1:
// Display dag uitslagen
nextView = 2;
MessageBox.Show("Enter case 1");
//command = new SqlCommand("XYZ");
break;
case 2:
// Display uitslagen per reeks
nextView = 0;
MessageBox.Show("Enter case 2");
//command = new SqlCommand("GetUitslagenPerReeks");
break;
}
}

private void DisplayRows()
{
if (dataGridView1.Rows.Count <
dataGridView1.DisplayedRowCount(false))
{
timer1.Enabled = false;
SimpleDelegate simpleDelegate = new SimpleDelegate(MainLoop);
simpleDelegate();
}

counter++;
if (counter * dataGridView1.DisplayedRowCount(false) <
dataGridView1.Rows.Count)
{
dataGridView1.CurrentCell = this.dataGridView1.Rows[counter
* dataGridView1.DisplayedRowCount(false)].Cells[0];
}
else
dataGridView1.CurrentCell =
this.dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0];

//counter = counter + dataGridView1.DisplayedRowCount(false);
//dataGridView1.CurrentCell =
this.dataGridView1.Rows[counter].Cells[0];
//dataGridView1.FirstDisplayedScrollingRowIndex +=
dataGridView1.DisplayedRowCount(false);

//}
//else
//{
// MessageBox.Show("eee");
// timer1.Enabled = false;
// SimpleDelegate simpleDelegate = new
SimpleDelegate(MainLoop);
// simpleDelegate();
//}

//dataGridView1.Refresh();

//SimpleDelegate simpleDelegate = new SimpleDelegate(MainLoop);
//simpleDelegate();
}

private void Form1_Load(object sender, EventArgs e)
{
InitializeDataGridView();
}

private void GetRows(SqlCommand command)
{
string sqlConnectString =
GetConnectionStringByName("XXXConnectionString");
using (SqlConnection connection = new
SqlConnection(sqlConnectString))
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
}
}

static string GetConnectionStringByName(string name)
{
return
ConfigurationManager.ConnectionStrings[name].ConnectionString;
}

private void button1_Click_1(object sender, EventArgs e)
{
MainLoop();
}

private void timer1_Tick(object sender, EventArgs e)
{
DisplayRows();
}
}
}
 
G

Guy

To clarify things a little bit more:

when having e.g. 36 rows in my datatable (datagrid.Rows.Count) the 4 first
iterations (with a DisplayedRowCount(false) of 8) take the correct
FirstDisplayedScrollingRowIndex (respective: 0, 8, 16, 24) but the last one
always takes the value 28 (there's no way to put 32). And one way or the
other there's some logic since 28 + 8 = 36!!?? but I want in the last
datagridview iteration only 4 rows displayed.

Thanks.


Guy said:
I'm trying to automatically, using a timer, refresh a DataGridView (see
attached code), but i don't succeed to display all rows correctly. I used
both FirstDisplayedScrollingRowIndex as well CurrentCell but doesn't work
(seeing all rows correctly). The delegate is only meant to call the MainLoop
method after all rows are displayed. Please can someone give me some help on
this, especially on the DisplayRows method? Many thanks.

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

namespace DigitalTournamentTracker
{
public partial class frmMain : Form
{
DataTable dt = null;
public delegate void SimpleDelegate();
int nextView = 0;
int counter = 1;

public frmMain()
{
InitializeComponent();
}

private void InitializeDataGridView()
{
DataGridViewRow row = this.dataGridView1.RowTemplate;
row.Height = 75;
//row.MinimumHeight = 35;

dataGridView1.ReadOnly = true;
dataGridView1.Font = new Font("Arial", 18, FontStyle.Bold);
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.None;
//dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.DisplayedCells;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
DataGridViewCellStyle dgvcsDef = new DataGridViewCellStyle();
dgvcsDef.WrapMode = DataGridViewTriState.True;
dgvcsDef.ForeColor = Color.Black;
dgvcsDef.BackColor = Color.MintCream;
dgvcsDef.Alignment = DataGridViewContentAlignment.TopCenter;
dataGridView1.DefaultCellStyle = dgvcsDef;

DataGridViewCellStyle dgvcsAlt = new DataGridViewCellStyle();
dgvcsAlt.WrapMode = DataGridViewTriState.True;
dgvcsAlt.ForeColor = Color.Black;
dgvcsAlt.BackColor = Color.LightGray;
dgvcsAlt.Alignment = DataGridViewContentAlignment.TopCenter;
dataGridView1.AlternatingRowsDefaultCellStyle = dgvcsAlt;
dataGridView1.RowHeadersVisible = false;

}

private void MainLoop()
{
MessageBox.Show("Enter MainLoop" + " " + nextView.ToString());
switch (nextView)
{
case 0:
// Display dag wedstrijdplanning
nextView = 1;
MessageBox.Show("Enter case 0");
SqlCommand command = new
SqlCommand("GetDagWedstrijdplanning");
command.Parameters.Add("@xyz", SqlDbType.DateTime).Value
= "2007-07-24";
GetRows(command);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].HeaderText = "Uur";
dataGridView1.Columns[1].HeaderText = "xxx";
dataGridView1.Columns[2].HeaderText = "yyy";
dataGridView1.Columns[3].HeaderText = "zzz";
dataGridView1.Columns[4].HeaderText = "ttt";
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[1].Width = 513;
dataGridView1.Columns[2].Width = 514;
dataGridView1.Columns[3].Width = 100;
dataGridView1.Columns[4].Width = 100;
//dataGridView1.FirstDisplayedScrollingRowIndex = 0;
timer1.Enabled = true;
break;
case 1:
// Display dag uitslagen
nextView = 2;
MessageBox.Show("Enter case 1");
//command = new SqlCommand("XYZ");
break;
case 2:
// Display uitslagen per reeks
nextView = 0;
MessageBox.Show("Enter case 2");
//command = new SqlCommand("GetUitslagenPerReeks");
break;
}
}

private void DisplayRows()
{
if (dataGridView1.Rows.Count <
dataGridView1.DisplayedRowCount(false))
{
timer1.Enabled = false;
SimpleDelegate simpleDelegate = new SimpleDelegate(MainLoop);
simpleDelegate();
}

counter++;
if (counter * dataGridView1.DisplayedRowCount(false) <
dataGridView1.Rows.Count)
{
dataGridView1.CurrentCell = this.dataGridView1.Rows[counter
* dataGridView1.DisplayedRowCount(false)].Cells[0];
}
else
dataGridView1.CurrentCell =
this.dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0];

//counter = counter + dataGridView1.DisplayedRowCount(false);
//dataGridView1.CurrentCell =
this.dataGridView1.Rows[counter].Cells[0];
//dataGridView1.FirstDisplayedScrollingRowIndex +=
dataGridView1.DisplayedRowCount(false);

//}
//else
//{
// MessageBox.Show("eee");
// timer1.Enabled = false;
// SimpleDelegate simpleDelegate = new
SimpleDelegate(MainLoop);
// simpleDelegate();
//}

//dataGridView1.Refresh();

//SimpleDelegate simpleDelegate = new SimpleDelegate(MainLoop);
//simpleDelegate();
}

private void Form1_Load(object sender, EventArgs e)
{
InitializeDataGridView();
}

private void GetRows(SqlCommand command)
{
string sqlConnectString =
GetConnectionStringByName("XXXConnectionString");
using (SqlConnection connection = new
SqlConnection(sqlConnectString))
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
}
}

static string GetConnectionStringByName(string name)
{
return
ConfigurationManager.ConnectionStrings[name].ConnectionString;
}

private void button1_Click_1(object sender, EventArgs e)
{
MainLoop();
}

private void timer1_Tick(object sender, EventArgs e)
{
DisplayRows();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top