Monday, August 16, 2010

Asp.net gridView to show data from Excel file...?

I'm using gridView to show data from Excel file.


one of the fields is an Email address,the problem is that


the gridView shows the Email with out the option of clicking


on it to send mail.


How can I define the gridView design so that the Email shown can be clickable?





Thanks


AsafAsp.net gridView to show data from Excel file...?
Oh boy, this is a VERY big topic. There are umpteenth ways to do this, and the best way depends on a lot of other stuff you're doing. I suggest you grab a copy of ';Data Binding with Windows Forms 2.0'; by Brian Noyes (Addison-Wesley), it goes into a lot of detail about all the ways to do stuff like this.





In the mean time, here's how to do it manually. Let's say you've got a table that you're binding to the GridView, I'll use the following code in Page_Load as an example, in your app you're obviously loading it from a spreadsheet:





if (!this.IsPostBack)


{


DataTable table = new DataTable(';Email addresses';);


table.Columns.Add(';UserName';, typeof(string));


table.Columns.Add(';Email';, typeof(string));


table.Rows.Add(';Tom';, ';tom@nospam.com';);


table.Rows.Add(';Dick';, ';dick@shoo.com';);


table.Rows.Add(';Harry';, ';harry@dontmailme.com';);


this.GridView1.DataSource = table;


this.GridView1.DataBind();


}





Open your web page in design mode, select the GridView control and bring up the tasks pane (it's the little right arrow triangle in the upper right hand corner of the control). Now click on Edit Columns and turn Auto-Generate Fields off. Now click the Add button to add a new BoundField column and set the ';Header Text'; and ';DataTextField'; fields to ';UserName';. Create another collumn called ';Email';, but make it of type ';Button Field';.





Now run your application and you'll notice that text in the Email column can be clicked on and it'll generate a postback. If you want an actual GUI button then go back to the column settings and change the ';ButtonType'; field to ';Button';.





Finally, you'll need to trap the post-back. Open up the column settings again and add a command to the ';CommandName'; field. If you set it to ';Select'; then you can add a handler to the GridView control's SelectedIndexChanging event and it'll get called whenever the user presses one of the buttons in the email field.





Good luck!Asp.net gridView to show data from Excel file...?
You might want to add a ButtonField to the columns collection for the GridView, or merely an achor in a TemplateField.





You can set the href attribute of the anchor to something like


%26lt;asp:TemplateField%26gt;


%26lt;ItemTemplate%26gt;


%26lt;a href=';mailto:%26lt;%# Eval(';Email) %%26gt;';%26gt; %26lt;%# Eval(';Email) %%26gt; %26lt;/a%26gt;


%26lt;/ItemTemplate%26gt;


%26lt;/asp:TemplateField%26gt;





Hope this helps

No comments:

Post a Comment