TZRick
asked on
Easy Question: Dynamically Formatting DataGrid columns
Hello experts!
I have a DataGrid that is bound to a custom-made DataSet. I would like to know how I can make one of the columns a column of LinkButtons programatically instead of just plaintext. I want to program an EventHandler to handle the LinkButton.Click events.
While we're at it...How can we programatically format a column's properties, e.g. alignment, font, etc?
Any help is appreciated.
Sincerely,
Taarik.
I have a DataGrid that is bound to a custom-made DataSet. I would like to know how I can make one of the columns a column of LinkButtons programatically instead of just plaintext. I want to program an EventHandler to handle the LinkButton.Click events.
While we're at it...How can we programatically format a column's properties, e.g. alignment, font, etc?
Any help is appreciated.
Sincerely,
Taarik.
Have you tried playing with the html side:
example:
<asp:datagrid >
and the many properties it has...... <BoundColumn>..... <HyperLinkColumn>........< TemplateCo lumn>..... .. etc...
example:
<asp:datagrid >
and the many properties it has...... <BoundColumn>..... <HyperLinkColumn>........<
DataSet ds = new DataSet ();
ds.Tables.Add (new DataTable ("TableName"));
object [] values;
ds.Tables [0].Columns.Add (new DataColumn ("Event", typeof (string)));
ds.Tables [0].Columns.Add (new DataColumn ("Expected Attendance", typeof (string)));
for (int counter = 0; counter < 8; counter++)
{
values = new object [] {{(getEventName ((ExtendedEvents)counter)) ,
getCountPerEvent ((ExtendedEvents) counter)};
ds.Tables [0].Rows.Add (values);
}
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "TableName";
DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "Event";
dgcs.Width = 80;
dgts.GridColumnStyles.Add( dgcs);
dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "Expected Attendance";
dgcs.Width = 80;
dgts.GridColumnStyles.Add( dgcs);
dgRSVP.TableStyles.Add(dgt s);
dgRSVP.DataSource = ds.Tables [0];
return;
'************************* ********** ********** ********** *********
'*******Then******
private void dgRSVP_MouseUp(object sender, System.Windows.Forms.Mouse EventArgs e)
{
string linkName = dgRSVP[dgRSVP.CurrentRowIn dex,1].ToS tring();
LinkLabel llbl = new LinkLabel();
llbl.Size = new System.Drawing.Size(30, 10);
llbl.AutoSize = true;
llbl.Text = linkName;
llbl.LinkClicked += new LinkLabelLinkClickedEventH andler(llb l_LinkClic ked);
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgR SVP.TableS tyles[0].G ridColumnS tyles[1];
dgtb.TextBox.Controls.Clea r();
dgtb.TextBox.Controls.Add( llbl);
}
just make sure you give you dataTable a name so the tablestyle knows what columns to map to.
ds.Tables.Add (new DataTable ("TableName"));
object [] values;
ds.Tables [0].Columns.Add (new DataColumn ("Event", typeof (string)));
ds.Tables [0].Columns.Add (new DataColumn ("Expected Attendance", typeof (string)));
for (int counter = 0; counter < 8; counter++)
{
values = new object [] {{(getEventName ((ExtendedEvents)counter))
getCountPerEvent ((ExtendedEvents) counter)};
ds.Tables [0].Rows.Add (values);
}
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "TableName";
DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "Event";
dgcs.Width = 80;
dgts.GridColumnStyles.Add(
dgcs = new DataGridTextBoxColumn();
dgcs.MappingName = "Expected Attendance";
dgcs.Width = 80;
dgts.GridColumnStyles.Add(
dgRSVP.TableStyles.Add(dgt
dgRSVP.DataSource = ds.Tables [0];
return;
'*************************
'*******Then******
private void dgRSVP_MouseUp(object sender, System.Windows.Forms.Mouse
{
string linkName = dgRSVP[dgRSVP.CurrentRowIn
LinkLabel llbl = new LinkLabel();
llbl.Size = new System.Drawing.Size(30, 10);
llbl.AutoSize = true;
llbl.Text = linkName;
llbl.LinkClicked += new LinkLabelLinkClickedEventH
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgR
dgtb.TextBox.Controls.Clea
dgtb.TextBox.Controls.Add(
}
just make sure you give you dataTable a name so the tablestyle knows what columns to map to.
ASKER
Thank you so much for your help ryeandi. Your code looks really ingenious. The only thing is that I am using a WebForm and objects such as DataGridTableStyle seem to be only in the Windows Forms package.
Thank You for your help!
Taarik.
Thank You for your help!
Taarik.
ASKER
CNTITDEPT...This DataGrid will be created dynamically. Could you expand a bit on the HTML please. HTML isn't one of my strong points right now. :-)
I think I am on the wrong path here... it looks like you guys are writing for a WinForm not a WebForm, but if it was a Webform it still could be done dynamically using the textwriter to write the html on the fly.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
private void eventsListing ()
{
DataSet ds = new DataSet ();
ds.Tables.Add (new DataTable ());
object [] values;
ds.Tables [0].Columns.Add (new DataColumn ("Event", typeof (string)));
ds.Tables [0].Columns.Add (new DataColumn ("Expected Attendance", typeof (string)));
for (int counter = 0; counter < 8; counter++)
{
values = new object [] {(getEventName ((ExtendedEvents)counter))
getCountPerEvent ((ExtendedEvents) counter)};
ds.Tables [0].Rows.Add (values);
}
dgRSVP.DataSource = ds.Tables [0];
dgRSVP.DataBind ();
return;
}