An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ column ’

In this solved challenge, I had a GridView on my C# Web Application that was dynamically creating columns based on user selections. In the general case, everything was working without any problems. The potential column names were listed in the database, which were selectable from a checkbox list.

One day, I got reports that the page was breaking (javascript error), but not blowing up completely. I narrowed down to the fact that one of the potential column names added to the database included a period (or dot). During the debugging phase, I found the following error, where in fact the column name should have been called ‘12.5′.

DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name ‘12′.

This was then causing the code that at runtime creates columns to error. The column seems to be successfully created, but the column name was only displaying as ‘12′ as opposed to ‘12.5′. As it turns out, the Eval function accepts a pattern, which the period (or dot) is assigned different meaning.

hl.Text = DataBinder.Eval(row.DataItem, columnName).ToString();

Through searching the MSDN libraries and the internet, I came across a similar solution where we assign the hyperlink via a different function. The GetPropertyValue function appears not to use a pattern search and instead searches for the string exactly as is passed in the function.

hl.Text = DataBinder.GetPropertyValue(row.DataItem, columnName).ToString();

Therefore, this was a solution to my problem. Through other various web developer forums and sites, I have also found that this problem applies not only to the period (or dot), but also to other special characters of the pattern. I believe dot means wildcard.

Trying to access a specific GridView’s column is especially easy, but only if you know the column index. One way to handle accessing the column is to hard code the column index into the code:

GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];

The main problem with the above solution is if you add a new column at index 1, you will need to manually change all hardcoded indexes. Therefore, another way to handle this is to set a constant integer for each column containing the index.

const int _MYCOLUMN = 5;
GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];

Using this would only require you to change the _MYCOLUMN constant integer in one location, which would reflect in the rest of your code. However, an even better way to handle this is to access the column by a defined name. The method below involves setting the AccessibleHeaderText of a column on the aspx page when the gridview column is defined.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   <Columns>
      <asp:BoundField AccessibleHeaderText="date" DataField="date" HeaderText="Date" SortExpression="date" />
      <asp:BoundField AccessibleHeaderText="article" DataField="article" HeaderText="Article" SortExpression="article" />
   </Columns>
</asp:GridView>

By setting the AccessibleHeaderText, we can now access the column through this header name. The function below requires the gridview and a string containing the AccessibleHeaderText. The function will return the index of the column, if found . Otherwise, negative one (-1).

private int findColumnIndex(GridView gridView, string accessibleHeaderText)
{
   for (int index = 0; index  < gridView.Columns.Count; index++)
   {
   if (String.Compare(gridView.Columns[index].AccessibleHeaderText, accessibleHeaderText, true) == 0)
      {
         return index;
      }
   }
   return -1;
}

By using this method, adding a new column in index one will not affect the rest of the code. The method checks the AccessibleHeaderText of each column to verify that it matches your specified string. If it matches, it returns the index.

int dateIndex = findColumnIndex(GridView1, "date");
int authorIndex = findColumnIndex(GridView1, "author");
int articleIndex = findColumnIndex(GridView1, "article");

In the above example, dateIndex will return 0, authorIndex will return -1, and articleIndex will return 1. It’s as simple as that!

MySQL, a free database engine, is a popular choice among webmasters. Despite its popularity, it is still difficult to visualize the database, table, fields, and data. Though there are many great php web interface solutions available, there are not many software based solutions. My favorite solution is Toad. It is powerful freeware for MySQL and available for download. If you have ever used Microsoft’s SQL Server software, the Toad user interface is will feel similar.

You are given a visual representation of your databases. Select a database and drill down to view the tables. From the tables, you can view either the existing data or view the columns. In addition to viewing the existing tables, you can create new tables, columns, and insert new data.

All in all, Toad is a great free option for MySQL users. If you have never given it a try, I highly recommend you give it a test drive. Included in the functionality is Version Control, Integration, Macro Record and Playback, Database Browser, Security Manager, Knowledge Xpert, Project Manager, SQL Recall, Code Formatter, Import/Export, Analyze Table, Check Table, Compare and Sync, Create/Drop Database, Kill Session, Change Database Variables, Customizing toolbars. Toad has been developed by Quest and can be found at http://www.quest.com/toad-for-mysql/