DataGrid Web Server Control
Posted March 28, 2002
Chat Date: March 19, 2002
Chat Participants:
- Omar Khan, Lead Program Manager
- Mike Pope, Programmer/Writer
- Susan Warren, Program Manager
MSDN_Erica
Welcome to today's MSDN Chat. Our topic is DataGrid Web Server Control. Questions, comments and suggestions are welcome.
MSDN_Erica
I'll let the hosts introduce themselves:
SWarren_MS
Hi --- I'm Susan Warren and I'm the program manager for the ASP.NET page framework, and also server controls in ASP.NET
SWarren_MS
let's here some questions -- fire away!
mpope_MS
My name is Mike Pope -- I work in Visual Studio User Education, documenting Web Forms.
OmarK_MS
Hi, I'm Omar Khan - I work on the Visual Studio Web Design team
SWarren_MS
Q: How do you get the ItemCommand Event to fire when you've added a column through code.
SWarren_MS
A: It should 'just work', provided the column exists early enough in the processing of the postback.
mpope_MS
A: qataredear - at this stage, there is no design-time support for using datareader with the grid (or any other control)
SWarren_MS
Q: Is it possible to display a thumbnail image in a DataGrid?
SWarren_MS
A: Sure, you can use a template column, and bind the path of the thumbnail image (images/someimage.gif for example)
SWarren_MS
Q: What is being done to fix the repeater flaw with grouped radio buttons in a datagrid?
SWarren_MS
A: This is definitely on our radar to add support for in a future release.
mpope_MS
Q: does (can) the DataGrid control use ODBC access?
mpope_MS
A: we do have an ODBC provider, but you have to install it; it's not part of the base install.
OmarK_MS
Q: I have a vb class file and i cant make a DLL out of it.. when I'm running the batch file it gives me error BC30002 ..Items not defined
OmarK_MS
A: Its most likely that the VBC.EXE compiler is not on your path. Try making sure its on the path. Otherwise try the visual basic compiler newsgroup.
SWarren_MS
Q: I need paging for a datagrid with button command column. I have "OnItemCommand" and
SWarren_MS
Q: "OnPageIndexChanged" for a datagrid, but "OnItemCommand" seems always override "OnPageIndexChanged",
SWarren_MS
Q: how can I get them to work at the same time?
SWarren_MS
A: Can you explain a little more what you are trying to get to? I don't think I've ever seen
SWarren_MS
A: someone put a "next page" button in every row of a grid!
SWarren_MS
Q: Is binding a DataGrid to a DataReader supported in ASP.NET? The web form designer doesn't seem to let me do this.
SWarren_MS
A: You *can* databind a DataReader to a DataGrid in code, btw.
SWarren_MS
**A:**Does that answer your question dennis?
mpope_MS
Q: I have a CheckBOX on my datagrid How I can detected which row was checked? I tried to use DataKeys, but it did not work for me
mpope_MS
A: ie32 is right -- you can walk the grid and extract the control, either by index or (preferably) by name and examine its checked value.
mpope_MS
A: There's an example in the white paper https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv\_vstechart/html/vbtchtopquestionsaboutaspnetdatagridservercontrol.asp
SWarren_MS
Q: I have a Web project w/ datagrid bound to an xml file (included in project). When I manually edit the XML file after debugging the project, I raise error:"The process cannot access the file because it is being used by another process. "...
SWarren_MS
A: I usually see this error when the XML file was not explicitly closed by the page code. It's still open (and locked) in the ASP.NET worker process. You can verify this by resetting the process (iisreset for example).
SWarren_MS
A: Bottom line -- its a bug in your page code. You must close the XML file explicitly.
OmarK_MS
Q: Mr. Khan it finds VBC.EXE .. I did added path %systemroot% \*....all that
OmarK_MS
A: I'm not sure what else it could be, you might want to try posting on the VB newsgroup
mpope_MS
Q: Binding the DataGrid to a DataReader in code does not enable me to use the DataGrid property builder. Is there a utiility function somewhere that converts a DataReader to a DataSet?
mpope_MS
A: Not as far as I know -- FWIW, we do plan to add design-time support for the reader in future.
mpope_MS
Q: So in order to click a column to sort on, the DataList has to be repopulated from either SQL, or viewstate, or session, correct? Objects do not persist between pages.
mpope_MS
A: exactly. Not very intuitive at first, is it?
SWarren_MS
Q: How far in the future are we talking for the repeater flaw to be resolved? Are there any workarounds that can be suggested with equal functionality?
SWarren_MS
A: We're looking at new functionality to support this (maybe a radio button column type) in our next release. We don't have a release date, but it's not next month
mpope_MS
Q: What kind of cashing mechanism exists behind the control? Is it customizable?
mpope_MS
A: the page framework does its own caching. You can "manually" cache by putting objects into Session or Application state. You can also use the OutputCache, which makes your caching subject to the cache's rules. Am I making sense?
mpope_MS
A: oh, and also by putting things into viewstate! Forgot.
SWarren_MS
Q: Is there a way to use the datagrid to do subgrouping - for example, I want to have a grid pf products, with breaks by category - and the tategory breaks will display an image, while the product detail will be table data.
SWarren_MS
A: yes, you can next a datgrid *within* the template column of a Datagrid. The trick is to bind the nested grid to a child relation of on the dataset. There's a good example of this in the "Black Belt Web Forms" archive on my web site:
SWarren_MS
https://www.swarren.net -- check out the dynamic templates samples
mpope_MS
Q: Why is viewstate enabled by default.. sounds like a bad practice
mpope_MS
A: Why do you say that? It's the mechanism by which controls can persist their property values.
mpope_MS
Q: mpope, but that bloats the viewstate by having it enabled on things that do not need to be persisted
mpope_MS
A: Ah. True, it does add to the download size. FWIW, it only persists non-default property values. If size is an issue, there is the option to turn it off...
SWarren_MS
Q: I have this code: <ASP: DataGrid id="MyDataGrid" ... OnItemCommand="MyDataGrid_Command" OnPageIndexChanged="MyDataGrid_Change" ...>, I added 2 button columns to the datagrid, one is "Edit", one is "Remove". but when I click the page number(I am using
SWarren_MS
A: the ItemCommand event fires for every button clicked within the grid *in addition* to the button-specific events.
SWarren_MS
A: The most likely cause of losing the event is that you are rebinding the grid on every postback. This resets
SWarren_MS
A: the grid to it's initial state, and you'll losing some events.
mpope_MS
Q: Can you multi-select items in your grid?
mpope_MS
A: not inherently. It would be an interesting exercise to come up with a clever way to do so, however. If no one else comes up with a way, I can investigate.
mpope_MS
Q: Can you multi-select items in your grid?
mpope_MS
A: not inherently. It would be an interesting exercise to come up with a clever way to do so, however. If no one else comes up with a way, I can investigate.
mpope_MS
oops, sorry for the repeat.
SWarren_MS
Q: Is there any way to customize the paging labels such as spacing, font, etc..... We would like to space the "Next" and "Prev" links apart instead of the default which is right next to each other or change the spacing between the page numbers?
SWarren_MS
A: Sure is. You can handle the grid's OnItemCreated command to monkey with the contents of the pager. Again, there's an example of this on https://www.swarren.net.
SWarren_MS
A: Check out the Black Belt Web Forms zip, and specifically the OnItemCreated samples.
mpope_MS
A: There's also an example in https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv\_vstechart/html/vbtchtopquestionsaboutaspnetdatagridservercontrol.asp
mpope_MS
Q: mpope, can you point me to that white paper again. I seem to have lost my transcript... thx
mpope_MS
A: I just reposted it in response to another question ... let me know if that isn't it.
OmarK_MS
Q: I'd like to know why you can't set the colspan for a column in a datagrid from an attribute - you have to use OnItemCreated and do some involved cell editing just to do something that should be so simple..
OmarK_MS
A: OnItemCreated is the way to do this, there is no support to edit it directly in the grid.
SWarren_MS
Q: On my Page I have a two userControl and each one has datagrid control on it, how I can fix them on the page or resize them
SWarren_MS
A: Usually, you would do this by positioning them within another element, like a table or panel.
mpope_MS
Q: how I get double sort with the title column link? I have now one way sort.
mpope_MS
A: do you mean toggle asc/desc or do you mean col1 followed byt col2?
SWarren_MS
Q: Can you multi-select items in your grid?
SWarren_MS
A: Yup, sure can. There's an example in the QuickStart samples that shows this: https://samples.gotdotnet.com (con't)
SWarren_MS
A: (con't) /quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx#editing
SWarren_MS
A: It's sorta the hotmail model though.
SWarren_MS
Q: It seems that I could only handle the page number click event in function "void MyDataGrid_Command(Object sender, DataGridCommandEventArgs e)", not in function "void MyDataGrid_Changed(Object sender, DataGridPageChangedEventArgs e)", is this a bug?
SWarren_MS
A: This *does* work... not sure why it isn't working for you *unless* you are rebinding the grid on postback and thereby blowing away the current state of paging.
mpope_MS
Q: Settle the debate... What is the true difference between a DataList and a DataGrid?
mpope_MS
A: Basically, datagrid is preformatted; datalist is essentially freeform -- you create the layout with templates. There's a topic "COntrols for Displaying Lists on Web Pages" that summarizes the differences.
mpope_MS
Q: Can I get access to a DataGrid's DataSet within OnItemDataBound (to say grab the value of an undisplayed field) ?
mpope_MS
A: You can get access to the current row using the DataItem of the current grid Item; it contains the complete data row for that item. Is that what you mean?
mpope_MS
Q: what's the easiest way to display data from an access table on the datagrid (vb.net)
mpope_MS
A: The only difference between Access and any other data source is that you use the OleDb provider and specify the Jet engine. Other than that, you can do something like what's in the VS topic "Displaying Data in a Web FOrms Page"
SWarren_MS
Q: Why is viewstate enabled by default.. sounds like a bad practice.... bloats the viewstate by having it enabled on things that do not need to be persisted
SWarren_MS
A: The point of viewstate is to persist the last-known UI state of the page. It may be "bloat" or it may be reducing load on the database server (in which case it is very valuable).
SWarren_MS
A: Some folks definitely think this *should* be enabled by default -- it's both a point of view and something a knowledgeable page developer can tune as they wish.
SWarren_MS
A: For more info, and strategies see the Viewstate articlie in the Nothing But ASP.NET column on MSDN (sorry can't find the link right now).
mpope_MS
Q: How can I have a "Confirm delete this row?" javascript message box from datagrid with validation controls when the user clicks on the delete button? I have tried AddAttribute method, but it doesnot work when you have validation controls.
mpope_MS
A: when you add a script call using AddAttribute, you need to delimit it with ";". Is the problem that the script doesn't run?
mpope_MS
A: Is it better to databind a control directly from a datareader or first to databind a datatable or dataset and then do binding from that - second version probably more flexible but noT as fast?
mpope_MS
A: if you're going to use a datatable, you might as well use a dataset -- it's populated using a datareader.
SWarren_MS
Q: Why can't a header or footer in a datalist or repeater be databound? Would be kinda useful if they could be..
SWarren_MS
A: You *can* assign data in a header or footer by handling the grid's OnItemCreated event. It doesn't really make sense to do regular databinding though, since databinding uses tabular data. Which record would you use for header or footer?
OmarK_MS
Q: Is there a hyperlinklist control ? Or a way to create a list of hyperlinks from a datasource without something like datalist templates?
OmarK_MS
A: No, its an interesting idea. However, the way to do this is to use a datalist or datagrid with a hyperlink control.
mpope_MS
Q: what's the easiest way to display data from an access table on the datagrid (vb.net)
mpope_MS
A: shakar, have a look at https://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdataingridinwebformspage.asp
SWarren_MS
Q: how I get double sort with the title column link? I have now one way sort.
SWarren_MS
A: The first trick is sort the data source -- for example, an ORDER BY somefield clause on the query, or the Sort property on a DataView.
SWarren_MS
A: The second trick is to persist the last sort value between roundtrips in viewstate -- there's an example of this in the Black Belt Web Forms
SWarren_MS
A: archive on https://www.swarren.net.
OmarK_MS
Q: Do you have plans to add an Insert Template, similar to edit template?
OmarK_MS
A: I'm not sure what you mean, is there a specific thing you are trying to accomplish.
SWarren_MS
Q: On the same topic: How do I dynamically display an outlook-style arrow in the column header showing which column is currently sorted, and whether ascending or descending?
SWarren_MS
A: once you know the sort direction, you can handle the OnItemCreated event and assign the "up arrow" or "down arrow" image url to an image control within the header.
OmarK_MS
Q: I have this line of code: <asp:BoundColumn HeaderText ="Start Date" DataField="MyStartDate"/>, in datagrid, it will print "Start" and "Date" in 2 seperate lines. But when I change it to <asp:BoundColumn ..."Start_Date"../>, then they are in one line. H
OmarK_MS
A: There is a wrap text property on the header formatting. If you set it to false, I think the header text should not wrap. Host
MSDN_Erica
We are going to have to wrap up this chat in a few minutes. If you asked a question a long time ago and it wasn't answered, please re-post now. We will get to as many as we possibly can.
mpope_MS
Q: Is there a way to tell in HTML if data in the datagrid has changed without incurring a roundtrip to the server? I would like to ask the user if they want to save if something has changed.
mpope_MS
A: FWIW, there's an example of checking for dirty rows in server code here: https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv\_vstechart/html/vbtchtopquestionsaboutaspnetdatagridservercontrol.asp
mpope_MS
Q: Scott: perhaps javascript could do this for you.. save values in the JS.. then compare field by field to the original values.. if nothing has changed, then the form's action attribute could be reset
mpope_MS
A: you'll need to add an client onchange handler for each editable control ...
mpope_MS
Q: would be good to get a simple run through of the life cycle of a datagrid - what happens first and last - databind, rendereing, itemcreated, gone out for coffee
mpope_MS
A: yeah, great suggestion. For the DG control and for the page as a whole ...
mpope_MS
A: ... and definitely that coffee thing ...
OmarK_MS
Q: what about bad opera 5 support though - datagrids look horrible
OmarK_MS
A: We are aware of browser support issues in this release and are working hard to make this better in future releases.
SWarren_MS
Q: Do you have plans to add an Insert Template, similar to edit template? we had a situation where we had an Add New Row button in the DG header, that inserted a blank row in the datagrid which was inserted into the database upon save.
SWarren_MS
A: So the short answer is no, we don't plan to add this functionality directly to the grid :p In most cases, you need to know more about the "rules" for the data than the datasource property provides to the grid.
SWarren_MS
A: For example, is the field nullable. I think the correct approach is to have a separate "add record" bit of UI, and not attempt to do this in place in the grid.
SWarren_MS
A: That way, you can add the appropriate validation rules via validator controls.
mpope_MS
Q: Can you DataBind to a method and pass the whole Container.DataItem to it? Passing it as a DataGridItem caused a casting error. This seems like it would be a very common thing
mpope_MS
A: Should work. Function you bind to has to be public and it must return a bindable object.
SWarren_MS
Q: LAST QUESTION: would be good to get a simple run through of the life cycle of a datagrid - what happens first and last - databind, rendereing, itemcreated, gone out for coffee
SWarren_MS
A: Excellent question -- it's a bit complicated, and a key topic for control authors. There's a good introduction in the powerpower deck in the
SWarren_MS
Authoring Controls, Basic Concepts zip on https://www.swarren.net. It's also in the framework docs, but the slide have color and animations :) Host
MSDN_Erica
Thanks for joining us today! You've asked some great questions. Unfortunately, it's time to go. You can find the transcript of this chat soon on the MSDN Web site at https://msdn.microsoft.com/chats/recent.asp
mpope_MS
Q: Hi... yes my method is public, and it returns a string. Still get "Specified cast is not valid."
mpope_MS
A: string isn't directly bindable, I think.
SWarren_MS
Ok, I guess that's it for this time! Be sure to check out www.asp.net for tons of great resources for developing with DataGrid. Thanks Everyone!
OmarK_MS
Thanks everyone for participating - glad to see folks out there using the grid. Thanks for the props IE32! ;-) Host
MSDN_Erica
Also, you can continue to post questions in our newsgroups: https://msdn.microsoft.com/newsgroups/
yag_MS
Also, see you all at next week's chat!
mpope_MS
Thanks! Great questions.
Top of Page