[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Easy Question: Dynamically Formatting DataGrid columns

Posted on 2004-09-28
8
Medium Priority
?
275 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:TZRick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 1

Author Comment

by:TZRick
ID: 12174008
So far, the grid is displayed with all values correct, etc. It's just the formatting now. Here is the code for DataSet and DataGrid creation:

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;
            }
0
 
LVL 1

Expert Comment

by:CNTITDEPT
ID: 12174647
Have you tried playing with the html side:

example:

<asp:datagrid >
and the many properties it has...... <BoundColumn>..... <HyperLinkColumn>........<TemplateColumn>....... etc...                                                                                                
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 12174692
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(dgts);                  
                             
dgRSVP.DataSource = ds.Tables [0];                  
               
return;
'****************************************************************
'*******Then******
private void dgRSVP_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
  string linkName = dgRSVP[dgRSVP.CurrentRowIndex,1].ToString();
  LinkLabel llbl = new LinkLabel();
  llbl.Size = new System.Drawing.Size(30, 10);
  llbl.AutoSize = true;
  llbl.Text = linkName;
  llbl.LinkClicked += new LinkLabelLinkClickedEventHandler(llbl_LinkClicked);

  DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgRSVP.TableStyles[0].GridColumnStyles[1];

  dgtb.TextBox.Controls.Clear();
  dgtb.TextBox.Controls.Add(llbl);            
}

just make sure you give you dataTable a name so the tablestyle knows what columns to map to.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:TZRick
ID: 12179471
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.
0
 
LVL 1

Author Comment

by:TZRick
ID: 12179591
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. :-)
0
 
LVL 1

Expert Comment

by:CNTITDEPT
ID: 12181568
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.
0
 
LVL 1

Accepted Solution

by:
TZRick earned 0 total points
ID: 12182686
You got me wrong CNTITDEPT. I did same that I am using a WebForm. Anyway, thanks for your input. I was able to get help to solve the problem and the solution is below.

Thank you again for your help.

Sincerely,
Taarik.

private void eventsListing ()
{
      Session ["event"] = null;
      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);
      }
      
      Session ["item"] = 0;                  
      dgRSVP.ItemCreated += new DataGridItemEventHandler (dgRSVP_ItemCreated);          
            
      dgRSVP.DataSource = ds.Tables [0];
      dgRSVP.DataBind ();
                  
      return;
}

private void dgRSVP_ItemCreated(object sender, DataGridItemEventArgs e)
{
      DataGrid dg = (DataGrid) sender;
      int count = (int)Session ["item"];
                  
      LinkButton lnkNumbers = new LinkButton ();
                  
      try
      {
            lnkNumbers.Text = dg.Items [count].Cells [1].Text;
            lnkNumbers.CssClass = "maintext";
            lnkNumbers.Click += new EventHandler (lnkNumbers_Click);
            lnkNumbers.ID = count.ToString ();
            dg.Items [count].Cells [1].Text = "";
            dg.Items [count].Cells [1].HorizontalAlign = HorizontalAlign.Center;
            dg.Items [count].Cells [1].Controls.Add (lnkNumbers);
            Session ["item"] = count + 1;
      }
      catch (Exception ex) {}

      return;
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question