Both Datasource and DatasourceID are defined on GridView - c#

I get the above error in my C#/SQL/ASP.NET app because I have a datasource defined both in the ASPX file and the ASPX.CS file. But I want the Gridview to have selectable rows. So if I comment out the ASPX.CS datasource I get the above error but if I comment out the ASPX datasource I get the Gridview output but it is not selectable. How do I code this successfully?

You should only be specifying one or the other. If you want to give the data object directly to the grid, set the DataSource to be that object.
Otherwise, set the DataSourceID to the ID of the datasource that you want to be binding to, and let it do it's thing.
Selectable rows shouldn't have anything to do with where the data is coming from. You can select a row by setting the SelectedRow property. Making them selectable in the UI is a whole different topic.

Sounds like you need to just set the DataSourceID from the markup, and set AutoGenerateSelectButton to true in your markup as well. Your question makes it sound like this is the behavior you are looking for. If you'd rather generate your own select buttons, you should look into either CommandFields, or just adding your own buttons with a CommandName of "Select".

Related

Telerik RadGrid using Multiple Columns

I am having a problem with the RadGrid using Multiple Columns when I Bind a DataSource to the RadGrid. Firstly let me show some screenshots of what I am trying to do.
This is an example from Teleriks website under demos and the multiple columns (summary column) is what I am trying to achieve
This is the exception I am getting when I try and bind the control to a DataSource
This is the RadGrid I have set up in my ASP page.
Firstly I don't want to bind the datasource in my asp page. Secondly I wanted the "Product" and "Other" ColumnGroups to be used only as a Summary column and not to be bound to a datasource since these ColumnGroups are only used as headers for the other Columns.
Is there a workaround to not allow the ColumnGroups not to be bound to a DataSource while the normal columns are?
Firstly, each one of your columns is specifying a ColumnGroupName that does not exist. Your two groups are called OtherDetails and ProductDetails not Product and Other. Change them to resolve the exception.
Secondly, the GridColumGroups are simply groups, as they are not bound to any data from your datasource; they are simply for aesthetics.

How to enable Selection for a GridView that assigns the DataSource in the code behind class?

I noticed that when I create a GridView that I am given the option to "Enable Selection" as long as I choose a DataSource in the GUI. However, I am binding the DataSource in the code behind class.
I added SelectMethod="GetCustomers" to my GridView control and an empty GetCustomers() method to my code behind class but I don't see a "Select" link appear in my GridView.
How can I enable "Selection" for my GridView if the DataSource is being bound in the code behind?
I should probably also mention that my data is coming from my NorthwindDAL.GetCustomers() method which is returning a List of type .
GridView1.AutoGenerateColumns = true;
GridView1.AutoGenerateSelectButton = true;
set the above properties to true and ofcourse, welcome to StackOVerFlow

ASP.NET: Programatically DataBind a GridView control

I have a blank/unbound GridView control on my form and I am binding it in the code behind like this:
GridView1.DataSource = _dataSet
DataBind()
Running this code populates my GridView control with all the columns and data that _dataSet has. I want to display only some of the columns, change the column names, and rearrange some of these columns too (i want the last column from the actual sql database table to be displayed first in my GridView).
Can someone show me how do do this?
Set the AutoGenerateColumns property of the GridView to false and manually create the columns. That would be the easiest way.
Another way is to also set AutoGenerateColums property to false but this time, append the colums to the columns property of the GridView. Use the .Clear and the Add methods provided
First of all, in your GridView markup, you can change the header text:
<asp:BoundField DataField="SOME_COLUMN" HeaderText="Comment" SortExpression="SOME_COLUMN" />
Be sure to set AutoGenerateColumns to false.
Second of all, you can filter using DataSource.FilterExpression = "col1 = 'this'", if your dataset is being populated by a SqlDataSource or similar.
I've written an article that focuses on different ways to create columns in a GridView. It also discusses how to change the name of a column etc. I hope it will be of use to those, who are new to the GridView: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns

ASP.NET C# - Removing a column from a datalist

I have a datalist inside a usercontrol that gets loaded into a page where users can customize a report based on some checkboxes.
One of the checkboxes, however, is "Hide Worklog" which should hide the worklog column from the result set because it can be quite long and interfere with the report.
If I do:
datatable1.Columns.Remove("WorkLog");
the code throws an exception because:
<asp:Label ID="WorkLog" runat="server" Text='<%# Bind("WorkLog") %>'></asp:Label></td>
doesn't exist.
Am I going about the usercontrol all wrong? This usercontrol should always be able to show the worklog, so I don't think it's bad to bind it in there, but at the same time I want to be able to hide it if the user wants.
Try removing the label control from your DataList instead of removing the column from the data source (i.e. the DataTable)
DataList1.Controls.Remove(DataList1.FindControl("WorkLog"));
You shouldn't get an error if the data source has more columns than you're displaying on the page, however, you will get an error, as you've discovered, if you're trying to display a column that doesn't exist in the data source.
bind it in code behind after checking some condition. like
if (visible) {
//bind
}
while removing control
visible = false;
you might need to change visible to session var :)

edit datagrid row

i want to know how to edit a single row (which i select) from a data grid
for example i have a datagrid with columns A, B and C
and i have a couple rows of data, approx 10 rows.
lets say i want to change the value of data within row 4.
how would i do this?
i am using visual studio 2003, but i guess if visual studio 2005 would be okay too. for the coding I'm using c#
thanks..
All grid-like components of asp.net have the same machanism as it comes to starting to edit a single row. Actually it's default for asp.net only to edit a single row in a grid.
Needed to start editing is to include asp:button or asp:linkbutton in the ItemTemplate with the CommandName set to "Edit". This one of reserved commandnames all grid-like components knows how to respond to. Clicking this button in DataGrid will raise the EditCommand Event. In this event you have to set the EditItemIndex of the grid equal to Item.Itemindex of the eventargs. This will render the row vaccordeing to the EditItemTemplate.
In this template you put 2 buttons or linkbuttons. One should have the CommandName set to "Update" and one should have the CommandName set to "Cancel".
The "Update" button raises the UpdateCommand event. In which you execute code that store the data in the row to its storage (eg.: database) and sets the EditItemIndex to -1 --> all rows are rendered read-only(ItemTemplate or AlternateItemTemplate).
The "Cancel" button raises the CancelCommand event. In the event handler you have to do si set the EditItemIndex to -1.
This description is only true for DataGrid en not for the in asp.net introduced GridView which handles most of this "Boilerplate"code it self working together with the datasource controls. Google the web for more info on this. it's to much to explain here right now.
Hope it helps?
Take a look at the documentation for adding an EditItemTemplate to your datagrid. You use the ItemTemplate for view-only, display elements and you use the EditItemTemplate for controls used to bind against a single row that you select.
Here's a link that might help:
http://www.gridviewguy.com/
Is your data in a DataTable before making it a DataGrid, or can you put it in a DataTable? You can update/delete/edit rows in a DataTable, here's a link with code snippets, pretty straight forward:
http://msdn.microsoft.com/en-us/library/tat996zc(VS.80).aspx

Categories

Resources