?
Solved

Displaying buttons in dynamicaly populatet GridView

Posted on 2008-10-29
3
Medium Priority
?
415 Views
Last Modified: 2011-10-19
I developed a wizard using ASP.net with which the user will be able to register requests of a certain type. Each request may have one or more contact persons, which the user will be able to add on a given step of the wizard. This step contains a form to add a new contact person and a table (GridView Control) which displays all contact persons the user already provided.

 

At the end of the whole wizard, all data will be saved in a database. Meanwhile I store the data (e.g. the contact persons) in a session variable. This session variable is a typed list of contact persons (List<ContactPerson>). On each page load all contact persons available in this session variable will be displayed in the table mentioned.

 

The requirement is to display two buttons in each row enabling the user to either edit or delete a contact person. Here is my problem: I am not able to create a table (currently a GridView Control, but may be anything else) from the data in the session variable and add the buttons (I now try to do so programmatically, but any solution is appreciated).


//Currently I use the following code in the *.aspx-file: 
 
 
 
<asp:GridView ID="GV_ContactPersons" runat="server" Width="100%">
 
</asp:GridView>
 
 
 
//& and the following code in the *.aspx.cs-file where you can see how I currently am converting the data in the session variable to the table (a DataTable which I then bind to the GridView): 
 
 
 
    private void FillDataGrid()
 
    {
 
      DataTable L_Table = new DataTable();
 
 
 
      DataColumn L_ColumnName = new DataColumn("Name");
 
      L_Table.Columns.Add(L_ColumnName);
 
 
 
      DataColumn L_ColumnAddress = new DataColumn("Adresse");
 
      L_Table.Columns.Add(L_ColumnAddress);
 
 
 
      DataColumn L_ColumnCity = new DataColumn("Ortschaft");
 
      L_Table.Columns.Add(L_ColumnCity);
 
 
 
      DataColumn L_ColumnPhone = new DataColumn("Telefon");
 
      L_Table.Columns.Add(L_ColumnPhone);
 
 
 
      DataColumn L_ColumnEMail = new DataColumn("E-Mail");
 
      L_Table.Columns.Add(L_ColumnEMail);
 
 
 
      //ButtonColumn L_ButtonColumn = new ButtonColumn();
 
      //ButtonField L_ButtonField = new ButtonField();
 
      //GridViewRow L_GridViewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
 
 
 
      try
 
      {
 
        foreach (ContactPerson L_ContactPerson in (List<ContactPerson>)Session["ContactPersons"])
 
        {
 
          //LinkButton L_EditButton = new LinkButton();
 
          //L_EditButton.Text = "Bearbeiten";
 
          //L_EditButton.CommandArgument = L_ContactPerson.ContactPerson_PK.ToString();
 
          //L_EditButton.Command += new CommandEventHandler(L_EditButton_Command);
 
 
 
          DataRow L_Row = L_Table.NewRow();
 
          //GridViewRow L_Row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
 
 
 
          L_Row[L_ColumnName] = L_ContactPerson.ContactPerson_Title + " " + L_ContactPerson.ContactPerson_FirstName + " " + L_ContactPerson.ContactPerson_LastName;
 
          L_Row[L_ColumnAddress] = L_ContactPerson.ContactPerson_Address + " " + L_ContactPerson.ContactPerson_Address2;
 
          L_Row[L_ColumnCity] = L_ContactPerson.ContactPerson_PostalCode + " " + L_ContactPerson.ContactPerson_City;
 
          L_Row[L_ColumnPhone] = L_ContactPerson.ContactPerson_Phone;
 
          L_Row[L_ColumnEMail] = L_ContactPerson.ContactPerson_EMail;
 
 
 
          L_Table.Rows.Add(L_Row);
 
        }
 
 
 
        GV_ContactPersons.DataSource = L_Table;
 
        GV_ContactPersons.DataBind();
 
      }
 
      catch (Exception L_Ex)
 
      {
 
        Session.Add("ErrorMessage", "Die Tabelle kann nicht angezeigt werden");
 
        Session.Add("ErrorDescription", L_Ex.Message);
 
 
 
        Response.Redirect("ErrorPage.aspx");
 
      }
 
    }

Open in new window

wizard-step-contactpersons.png
0
Comment
Question by:Martin Kreis
[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
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 2000 total points
ID: 22838603
You can define your gridview control in aspx page as usual. I mean there is no real need to create columns in code (see code snippet). Also in same aspx you can add a template filed with buttons in it for editing/deleting.

Gridview control can bind not only to datatable or dataset. If binds to typed list (List) as well. So when it comes to showing data you just assign your list to gridview as datasource and call DataBind method (see snippet). You just have to make sure that DataField property of a BoundField in aspx matches a property of ContactPerson class. In a given sample in a snippet it is assumed that ContactPerson class has a public property Name.

HTH
R
// aspx
<asp:GridView ID="GV_ContactPersons" runat="server" Width="100%">
  <Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" />
    ...
  </Columns>
</asp:GridView>
 
// cs
this.GV_ContactPersons.DataSource = (List<ContactPerson>)Session["myData"];
this.GV_ContactPersons.DataBind();

Open in new window

0
 

Author Closing Comment

by:Martin Kreis
ID: 31511139
Thank you very much!!
0
 

Author Comment

by:Martin Kreis
ID: 22839420
Thank you very much for your help!
With your input, I was now able to solve my problem (with TemplatesFields).

Best regards
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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