Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, December 5, 2009 10:03 PM
Hi all, I have a really annoying issue with a button cell in a DataGridView control. I'm binding the grid to a dataset at runtime. Some of the rows in the grid will be linked to pdf documents. I create a button column and add it to the grid, and set the text of the cell in the button column based on the value of another column. When I step through the code I can see the ColumnIndex of the button column is 10, and the code correctly sets the text of the button cells I want. However when the form appears, the button text values for the rows I want are blank. When I click the button I check in the CellContentClick event to see if the ColumnIndex is 10 (which is the button column) it tells me the ColumnIndex is 0, even though it's the last column. Then when I reload the grid I call the BindHistoryGrid method again which drops the column if it exists and re-adds it. This time it sets the button text correctly. Is there some strange behavior going on that I can't see? How do I set the button ColumnIndex to 10 the first time I add it (even though it tells me that it's 10)?
private DataGridViewButtonColumn PDFButtonColumn;
private void BindHistoryGrid()
{
dataGridViewStmt.DataSource = ah.getAccountHistory(0, dateTimePicker1.Value, dateTimePicker2.Value);
if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");
dataGridViewStmt.Columns[0].DisplayIndex = 0;
dataGridViewStmt.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns[0].Visible = false;
dataGridViewStmt.Columns[1].Visible = false;
dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells[9].Value) > 0)
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "";
}
}
}
private void dataGridViewStmt_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 10 && dataGridViewStmt.CurrentRow.Cells[6].Value != System.DBNull.Value)
{
string pdfFile = "";
int docID = 0;
pdfFile = (string)dataGridViewStmt.CurrentRow.Cells[5].Value + ".pdf";
docID = (int)dataGridViewStmt.CurrentRow.Cells[9].Value;
if (docID > 0)
{
getPDFFile(docID, pdfFile, "pdf");
}
else
{
MessageBox.Show("No invoice available for this item";
}
}
}
All replies (29)
Sunday, December 6, 2009 5:59 AM
Hi,
If this the exacy code you are using
private DataGridViewButtonColumn PDFButtonColumn; will throw exception.
I checked and see that it works fine.
Here is my example.
public DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name1", typeof(string));
dt.Columns.Add("Name2", typeof(string));
dt.Columns.Add("Name3", typeof(string));
dt.Columns.Add("Name4", typeof(string));
dt.Columns.Add("Name5", typeof(string));
dt.Columns.Add("Name6", typeof(string));
dt.Columns.Add("Name7", typeof(string));
dt.Columns.Add("Name8", typeof(string));
dt.Columns.Add("Name9", typeof(int));
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i);
}
return dt;
}
private DataGridViewButtonColumn PDFButtonColumn=new DataGridViewButtonColumn();
private void BindHistoryGrid()
{
dataGridView1.DataSource = GetData();
if (dataGridView1.Columns["GetPDFFile"] != null)
dataGridView1.Columns.Remove("GetPDFFile");
dataGridView1.Columns[0].DisplayIndex = 0;
dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Visible = false;
dataGridView1.Columns.Add(PDFButtonColumn);
dataGridView1.RowHeadersVisible = false;
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells[9].Value) > 0)
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "";
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 10 && dataGridView1.CurrentRow.Cells[6].Value != System.DBNull.Value)
{
string pdfFile = "";
int docID = 0;
pdfFile = (string)dataGridView1.CurrentRow.Cells[5].Value + ".pdf";
docID = (int)dataGridView1.CurrentRow.Cells[9].Value;
if (docID > 0)
{
//getPDFFile(docID, pdfFile, "pdf");
MessageBox.Show("Here is the pdf");
}
else
{
MessageBox.Show("No invoice available for this item");
}
}
}
private void imgDispForm_Load(object sender, EventArgs e)
{
BindHistoryGrid();
}
Sunday, December 6, 2009 2:22 PM
Hi Tamer thanks for the reply. I'm initializing the button in the form load event, as in below:
PDFButtonColumn = new DataGridViewButtonColumn();
PDFButtonColumn.Name = "GetPDFFile";
When the BindhistoryGrid function is called the first time from the form load event it appears to work fine i.e. when I step through the code as it's called from the form load it correctly identifies the rows for which I want to set the button value, and it sets the text value of the button without any problems. It also has the Index of the button column as 11:
((DataGridViewButtonCell)(row.Cells[11])).Value = "Get Invoice";
The only thing is that the text doesn't appear when the form finishes loading - all the buttons are blank. When I click on one of the buttons and step through the code in the CellContentClick event, it tells me that the ColumnIndex of the Button cell is 0, even though it was 11 when I was stepping through the code in the BindHistoryGrid function.
The grid gets updated when an Update button is clicked. I call the BindHistoryGrid function again from a button click event. The only thing that's different this time is that the button column already exists, so it is dropped. The code carries on as normal and sets the button text on the desired rows again. This time however the text appears on the buttons that I want i.e. it works properly. When I click on one of the buttons this time, it tells me the ColumnIndex of the button is 11, which is what I expect.
So it looks like the first time the Button column is added it is somehow setting the ColumnIndex to 0, even though it is added to the end of the grid after the grid has been bound to a Dataset and the other columns have been set. Then when the Button column is dropped and re-added it sets the ColumnIndex to 11, which is what I expect. This isn't making much sense to me at all...can anyone shed more light on this?
Thanks,
Ciaran.
Sunday, December 6, 2009 2:25 PM
Hi,
Do you add the Column after binding data?(at the first time at form's load)
I think that's the problem.
Sunday, December 6, 2009 3:56 PM
Hi Tamer,
Ok I changed the code to add the Button column before I set the Datasource of the grid. I also reference the columns in the grid by name now rather than index. Still no luck though, the first time the grid loads the buttons are still blank, and after clicking the Update button the text appears. I see what you're saying though, adding the Button column before binding to the Dataset will leave the ColumnIndex at 0, so at least that is consistent now. I'm flummoxed by this, really can't figure out if it's a bug in the grid or if it's something dumb that I'm doing or failing to do. Here's the latest code:
AccountHistory ah;
private DataGridViewButtonColumn PDFButtonColumn;
private void formAccountHistory_Load(object sender, EventArgs e)
{
PDFButtonColumn = new DataGridViewButtonColumn();
PDFButtonColumn.Name = "GetPDFFile";
ah = new AccountHistory();
try
{
BindHistoryGrid();
}
catch (Exception ex)
{
Util.ExceptionUtility.LogException(ex, "formAccountHistory - Form_Load");
}
}
private void BindHistoryGrid()
{
if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");
dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.DataSource = ah.getAccountHistory(formMain.channel.channelID, dateTimePicker1.Value, dateTimePicker2.Value);
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns["AccountHistoryID"].Visible = false;
dataGridViewStmt.Columns["ChannelID"].Visible = false;
dataGridViewStmt.Columns["DocID"].Visible = false;
dataGridViewStmt.Columns["ActionType"].Visible = false;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Format = "0.00";
//dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.Columns["GetPDFFile"].DisplayIndex = 10;
dataGridViewStmt.Columns["GetPDFFile"].Width = 100;
dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells["DocID"].Value) > 0)
{
this.dataGridViewStmt["GetPDFFile", row.Index].Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells["GetPDFFile"])).Value = "";
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
BindHistoryGrid();
}
Sunday, December 6, 2009 4:10 PM
Hi,
Try this.
AccountHistory ah;
private DataGridViewButtonColumn PDFButtonColumn;
private void formAccountHistory_Load(object sender, EventArgs e)
{
PDFButtonColumn = new DataGridViewButtonColumn();
PDFButtonColumn.Name = "GetPDFFile";
ah = new AccountHistory();
try
{
BindHistoryGrid();
}
catch (Exception ex)
{
Util.ExceptionUtility.LogException(ex, "formAccountHistory - Form_Load");
}
}
private void BindHistoryGrid()
{
if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");
dataGridViewStmt.DataSource = ah.getAccountHistory(formMain.channel.channelID, dateTimePicker1.Value, dateTimePicker2.Value);
dataGridViewStmt.Columns.Add(PDFButtonColumn);<CHANGED THIS ROW. MOVED IT TO HERE
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns["AccountHistoryID"].Visible = false;
dataGridViewStmt.Columns["ChannelID"].Visible = false;
dataGridViewStmt.Columns["DocID"].Visible = false;
dataGridViewStmt.Columns["ActionType"].Visible = false;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Format = "0.00";
//dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.Columns["GetPDFFile"].DisplayIndex = 10;
dataGridViewStmt.Columns["GetPDFFile"].Width = 100;
dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells["DocID"].Value) > 0)
{
this.dataGridViewStmt["GetPDFFile", row.Index].Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells["GetPDFFile"])).Value = "";
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
BindHistoryGrid();
}
Sunday, December 6, 2009 4:19 PM
No joy I'm afraid, still behaving the exact same :(
Sunday, December 6, 2009 4:25 PM
It's intresting.
Could you try to give this as datasource to your code.
I think the data might be causing the problem.
You may need to change the columsn in datatable.
public DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name1", typeof(int));
dt.Columns.Add("Name2", typeof(int));
dt.Columns.Add("Name3", typeof(int));
dt.Columns.Add("Name4", typeof(int));
dt.Columns.Add("Name5", typeof(int));
dt.Columns.Add("Name6", typeof(int));
dt.Columns.Add("Name7", typeof(int));
dt.Columns.Add("Name8", typeof(int));
dt.Columns.Add("Name9", typeof(int));
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i, i, i, i, i, i, i, i, i);
}
return dt;
}
Sunday, December 6, 2009 5:33 PM
Ok I set the datasource of the grid to your GetData function. I changed the function slightly so the columns matched the columns in the database table I'm selecting from, as detailed below. Still the same result!
public DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("AccountHistoryId", typeof(int));
dt.Columns.Add("ChannelID", typeof(int));
dt.Columns.Add("Week", typeof(string));
dt.Columns.Add("Period", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("DR", typeof(Double));
dt.Columns.Add("CR", typeof(Double));
dt.Columns.Add("Balance", typeof(Double));
dt.Columns.Add("DocID", typeof(int));
dt.Columns.Add("ActionType", typeof(Double));
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i, i, i, DateTime.Today, i, (Double)i, (Double)i, (Double)i, i, (Double)i);
}
return dt;
}
Sunday, December 6, 2009 5:54 PM
Hi,
Sorry but this works fine for me. I tried both with autogenerated columns and declared columns.
Here is my code.
Form1.cs
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.Runtime.InteropServices;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("AccountHistoryId", typeof(int));
dt.Columns.Add("ChannelID", typeof(int));
dt.Columns.Add("Week", typeof(string));
dt.Columns.Add("Period", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("DR", typeof(Double));
dt.Columns.Add("CR", typeof(Double));
dt.Columns.Add("Balance", typeof(Double));
dt.Columns.Add("DocID", typeof(int));
dt.Columns.Add("ActionType", typeof(Double));
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i, i, i, DateTime.Today, i, (Double)i, (Double)i, (Double)i, i, (Double)i);
}
return dt;
}
private DataGridViewButtonColumn PDFButtonColumn;
private void Form1_Load(object sender, EventArgs e)
{
PDFButtonColumn = new DataGridViewButtonColumn();
PDFButtonColumn.Name = "GetPDFFile";
try
{
BindHistoryGrid();
}
catch (Exception ex)
{
}
}
private void BindHistoryGrid()
{
if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");
dataGridViewStmt.DataSource = GetData();
dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns["AccountHistoryID"].Visible = false;
dataGridViewStmt.Columns["ChannelID"].Visible = false;
dataGridViewStmt.Columns["DocID"].Visible = false;
dataGridViewStmt.Columns["ActionType"].Visible = false;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["DR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["CR"].DefaultCellStyle.Format = "0.00";
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridViewStmt.Columns["Balance"].DefaultCellStyle.Format = "0.00";
//dataGridViewStmt.Columns.Add(PDFButtonColumn);
dataGridViewStmt.Columns["GetPDFFile"].DisplayIndex = 10;
dataGridViewStmt.Columns["GetPDFFile"].Width = 100;
dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;
foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells["DocID"].Value) > 0)
{
this.dataGridViewStmt["GetPDFFile", row.Index].Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells["GetPDFFile"])).Value = "";
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
BindHistoryGrid();
}
}
}
//form1.designer.cs
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
this.mVPDataSet = new WindowsFormsApplication1.MVPDataSet();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.button1 = new System.Windows.Forms.Button();
this.dataGridViewStmt = new System.Windows.Forms.DataGridView();
this.AccountHistoryId = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ChannelID = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Week = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Period = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Date = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Description = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.DR = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.CR = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Balance = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.DocID = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ActionType = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.mVPDataSet)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewStmt)).BeginInit();
this.SuspendLayout();
//
// bindingSource1
//
this.bindingSource1.DataSource = this.mVPDataSet;
this.bindingSource1.Position = 0;
//
// mVPDataSet
//
this.mVPDataSet.DataSetName = "MVPDataSet";
this.mVPDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(0, 0);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
this.dateTimePicker1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(329, 299);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.btnUpdate_Click);
//
// dataGridViewStmt
//
this.dataGridViewStmt.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewStmt.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.AccountHistoryId,
this.ChannelID,
this.Week,
this.Period,
this.Date,
this.Description,
this.DR,
this.CR,
this.Balance,
this.DocID,
this.ActionType});
this.dataGridViewStmt.Location = new System.Drawing.Point(120, 27);
this.dataGridViewStmt.Name = "dataGridViewStmt";
this.dataGridViewStmt.Size = new System.Drawing.Size(629, 150);
this.dataGridViewStmt.TabIndex = 3;
//
// AccountHistoryId
//
this.AccountHistoryId.DataPropertyName = "AccountHistoryId";
this.AccountHistoryId.HeaderText = "AccountHistoryId";
this.AccountHistoryId.Name = "AccountHistoryId";
//
// ChannelID
//
this.ChannelID.DataPropertyName = "channelID";
this.ChannelID.HeaderText = "ChannelID";
this.ChannelID.Name = "ChannelID";
//
// Week
//
this.Week.DataPropertyName = "Week";
this.Week.HeaderText = "Week";
this.Week.Name = "Week";
//
// Period
//
this.Period.DataPropertyName = "Period";
this.Period.HeaderText = "Period";
this.Period.Name = "Period";
//
// Date
//
this.Date.DataPropertyName = "Date";
this.Date.HeaderText = "Date";
this.Date.Name = "Date";
//
// Description
//
this.Description.DataPropertyName = "Description";
this.Description.HeaderText = "Description";
this.Description.Name = "Description";
//
// DR
//
this.DR.DataPropertyName = "DR";
this.DR.HeaderText = "DR";
this.DR.Name = "DR";
//
// CR
//
this.CR.DataPropertyName = "CR";
this.CR.HeaderText = "CR";
this.CR.Name = "CR";
//
// Balance
//
this.Balance.DataPropertyName = "Balance";
this.Balance.HeaderText = "Balance";
this.Balance.Name = "Balance";
//
// DocID
//
this.DocID.DataPropertyName = "DocID";
this.DocID.HeaderText = "DocID";
this.DocID.Name = "DocID";
//
// ActionType
//
this.ActionType.DataPropertyName = "ActionType";
this.ActionType.HeaderText = "ActionType";
this.ActionType.Name = "ActionType";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(905, 468);
this.Controls.Add(this.dataGridViewStmt);
this.Controls.Add(this.button1);
this.Controls.Add(this.dateTimePicker1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.mVPDataSet)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewStmt)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.BindingSource bindingSource1;
private MVPDataSet mVPDataSet;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridViewStmt;
private System.Windows.Forms.DataGridViewTextBoxColumn AccountHistoryId;
private System.Windows.Forms.DataGridViewTextBoxColumn ChannelID;
private System.Windows.Forms.DataGridViewTextBoxColumn Week;
private System.Windows.Forms.DataGridViewTextBoxColumn Period;
private System.Windows.Forms.DataGridViewTextBoxColumn Date;
private System.Windows.Forms.DataGridViewTextBoxColumn Description;
private System.Windows.Forms.DataGridViewTextBoxColumn DR;
private System.Windows.Forms.DataGridViewTextBoxColumn CR;
private System.Windows.Forms.DataGridViewTextBoxColumn Balance;
private System.Windows.Forms.DataGridViewTextBoxColumn DocID;
private System.Windows.Forms.DataGridViewTextBoxColumn ActionType;
}
}
Monday, December 7, 2009 12:31 AM
I ran the code there again, still the same. Are there any design time settings or properties that would cause this problem? I also deleted the grid from the form and added it again. I didn't set any design time properties, just set the name the same as the original...still the same though.
Monday, December 7, 2009 7:28 AM
I think you have to call control.Refresh() ( control is your control object)after setting the text each time.
Monday, December 7, 2009 9:34 AM
Hi Mike, tried the Refresh method, didn't work unfortunately.
Monday, December 7, 2009 1:40 PM
Tamer - in the code in your last post you seem to have declared the columns at design time, and you are also using a BindingSource. Would this make a difference?
Monday, December 7, 2009 2:34 PM
Hi,
I did not use BindingSource in your example that ws on my form for another example.
And I tried boty declaring columns at design time and using autogenerated columns. Both works fine for me.
Friday, December 11, 2009 3:37 AM
Hi akaCiaran,
Have you solved the problem?
If you not , could you please upload a project that can help us to reproduce the scenario?
HarryPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Friday, December 11, 2009 12:18 PM
Hi Harry,
Still having the problem unfortunately. I have taken the code from Tamer above which works when I paste into a new form, but deleting the grid and adding a new grid (with the same name) in my existing form still has the problem. How do I upload a project? Can I email it directly to you somehow? (I'm a bit reluctant to post my full project code online).
Thanks,
Ciaran.
Monday, December 14, 2009 2:01 AM
Hi,
If you do not want to upload the project, just a simple project that can show the problem is helpful.
You might want to upload it to http://skydrive.live.com and please post the link to the project.
Thanks,
HarryPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Monday, December 14, 2009 11:02 AM
Ok I've zipped up the Winforms project and posted it below:
http://cid-b398a360a9eef819.skydrive.live.com/self.aspx/.Public
There is a function called GetData which returns a DataTable. This simulates what would come back from the web services call, which is what populates the data grid. There are 2 functions called BindHistoryGrid and BindHistoryGrid2. BindHistoryGrid is the original one that sets the text on the buttons based on a value in a certain column being greater than 0. Just to recap, the function doesn't set the text the first time it is called from the form load, only when it is called a second time from the Update button.
I created a second function called BindHistoryGrid2 that uses a DataGridViewDisableButtonColumn control. In this function I also set the text of the button column but hide the button when the value of a particular cell is 0. Again the buttons are not hidden the first time the function is called, only when it is called a second time from the Update button.
Any help or ideas much appreciated.
Thanks,
Ciaran.
Monday, December 14, 2009 4:53 PM
Ok the plot thickens. I created a new form, which is a simple form with a grid and button. In the form constructor I call a function to bind the grind to a DataTable, and then loop through the rows setting the background colour of the last cell to LightGrey and the cell itself to read-only if the column value is true. After the form finishes loading the code didn't work i.e. the cells are not set to LightGrey and are not read-only (even though when I step through the code I can see the properties being set). I then call the function again from the button, but this time the colour is changed to LightGrey and the cell is made read-only i.e. the code works.
I really don't understand this - why doesn't it do it the first time when the function is called as the form loads? Does the grid repaint itself somehow and reset any values that I set when the form finishes loading? Here's the form code:
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;
namespace WindowsFormsApplication1
{
public partial class formOrderReview : Form
{
//public formOrderReview()
//{
// InitializeComponent();
//}
DataGridView dataGridView1 = new DataGridView();
Button button1 = new Button();
public formOrderReview()
: base()
{
Controls.Add(dataGridView1);
Controls.Add(button1);
button1.Click += new EventHandler(button1_Click);
button1.Top = 175;
button1.Left = 300;
button1.Text = "Reset";
dataGridView1.Width = 450;
this.Width = 750;
this.Height = 400;
DisplayOrderDetails();
}
private void DisplayOrderDetails()
{
dataGridView1.DataSource = GetOrderData("");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Cells["IsDeleted"].Value) != null && (row.Cells["IsDeleted"].Value) != DBNull.Value)
{
if (((bool)row.Cells["IsDeleted"].Value) == true)
{
row.Cells[dataGridView1.ColumnCount - 1].Value = 1;
row.Cells[dataGridView1.ColumnCount - 1].ReadOnly = true;
row.Cells[dataGridView1.ColumnCount - 1].Style.BackColor = Color.LightGray;
}
}
}
}
public DataTable GetOrderData(string dummy)
{
DataTable dt = new DataTable();
dt.Columns.Add("OrderTicketID", typeof(int));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Section", typeof(string));
dt.Columns.Add("Row", typeof(string));
dt.Columns.Add("Seat", typeof(string));
dt.Columns.Add("IsDeleted", typeof(bool));
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i, "Description " + i, "Section " + i, "Row " + i, "Seat " + i, i);
}
return dt;
}
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new formOrderReview());
}
private void button1_Click(object sender, EventArgs e)
{
DisplayOrderDetails();
}
}
}
Tuesday, December 15, 2009 9:50 AM
Hi,
I downloaded the project , but there are several forms in it. I tried open the formOrderReview form , but some data is not available.
Could you please simplify the project , so that we can see the problem with no difficult?
HarryPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Tuesday, December 15, 2009 2:36 PM
Hi Harry,
No problem I'll take out some of the forms and repost it. In the meantime the code in my last post also has the problem. You can paste this into a form and run the form, then reload the grid by clicking the button. The read-only and colour properties are not set when the form loads, only when the function is called for the second time by clicking the button.
Thanks,
Ciaran.
Wednesday, December 16, 2009 9:49 AM
Hi,
The constructor is executed very early in the form lifecycle, before any of the form's events (including Load) are fired. The form won't be completely initialised, so there will be some limitations to what you can do.
You might need to call the method in form load event handler:
void formOrderReview_Load(object sender, EventArgs e)
{
DisplayOrderDetails();
}
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Sunday, December 20, 2009 11:49 PM
Hi,
I've created a new project with some forms that have grids. The code works in certain situations now. If the grid is on the form directly, or is on the first tab of a tab control with 2 tabs it works. But if the grid is on the second tab it doesn't work. Form1 has the grid on the 2nd tab and it doesn't work. Form2 has it on the 1st tab and it works.
Form3 uses a constructor and loads the grid from this, but it doesn't work. formOrderReview has a grid directly on the form and is loaded from the form load and it now works.
In the program.cs file in the app swap out the different forms to see the different results.
In my main project I have a form with a grid directly on the form. When I load it from form Load the properties that I set on a column don't work, I have to call the function that sets the properties from the form Shown event to get it to work. So there's definitely something strange going on with the grid and i can't really figure it out.
http://cid-b398a360a9eef819.skydrive.live.com/browse.aspx/.Public/WinformApp
Monday, December 21, 2009 9:29 AM
Hi,
Could you please be more specific about the problem?
Which form does the issue arise?
I opened the form2 and found the back color is chaged.
HarryPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Monday, December 21, 2009 10:13 AM
Hi,
The issue arises in Form1, when the grid is on the 2nd tab of a tab control. When it's placed on the 1st tab it works. The BindHistoryGrid method, which sets the button properties, is called from Form load and again from a button. The problem is that the back colour and read-only properties (also the text property) are not being set on buttons in the button column, when BindHistoryGrid is called from form Load. However it works fine when called a second time from the button Click. Load up Form1 in the program.cs file and you'll see the problem.
Thanks,
Ciaran.
Tuesday, December 22, 2009 2:30 AM
Hi,
DataGridView won't refresh when it's not displaied to save resource.
Please try the following steps:
1 run the application.
2 click the button.
3 select tabpage2.
You can found the layout is not changed.
but if we select the tabpage2 , then select tabpage1 .
the button will refresh the layout.
The datagridview does not get initialized before we select tabpage2.
You might want to put the setting code into SelectedIndexChanged and check if the page2 is selected:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 1)
{
BindHistoryGrid();
}
}
Harry Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Tuesday, December 22, 2009 2:54 PM
Hi Harry,
--DataGridView won't refresh when it's not displaied to save resource.
Is this standard behaviour with the DataGridView control? I haven't seen this documented anywhere or it hasn't come up in any other online forum. Do you have any links to where this might be documented or explained, or to some sample code that shows how it works?
Thanks,
Ciaran.
Thursday, February 18, 2010 2:54 PM | 2 votes
Hi
set UserColumnTextForButtonValue property to true of your buttoncolumn
btncol.UseColumnTextForButtonValue =
True
Tuesday, August 5, 2014 11:19 AM
UseColumnTextForButtonValue Solved my issue.. why is this only after so long discussion? :)