i have code of grid view in aspx.cs it show an error, i cannot understand how it solve, So please anyone can help me? Error picture is attached.
A GridView can just have one DataSource. Since you are setting the DataSource programmatically remove the DataSourceID from the aspx part since that is used for declarative datasource controls like SqlDataSource or ObjectDataSource.
For example:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource" <-- !!! REMOVE THIS !!!
autogeneratecolumns="False"
emptydatatext="No data available."
allowpaging="True"
runat="server" DataKeyNames="CustomerID">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Columns>
</asp:gridview>
I think you have applied datasource Id from Aspx page to Grid.
like,
DataSourceID="DatasourceId"
Use one only.
go in your default.aspx, find the tag that contains the attribute ID="GridView1" and wipe from it the attribute DataSourceID="[SOMETHING]"
Related
I new to telerik and fighting my way up the learning curve.
I have a RadGrid that I'm populating with a linq query. I'm using a
GridTemplateColumn with a ComboBox for new and edit of one of the fields. The problem is the ComboBox doesn't show on the insert or edit screen. The fields set as GridDropDownColumn do show on the insert or edit. I need GridTemplateColumn solution because I need to run some code once the dropdown list has been selected.
What am I missing here? I'm trying to work from a Telerik example. It's the Release field that is giving me the problem.
<telerik:RadGrid RenderMode="Lightweight" ID="grdData" runat="server"
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
AllowAutomaticInserts="true"
AllowAutomaticUpdates="true"
OnNeedDataSource="grdData_OnNeededDataSource"
OnItemDataBound="grdData_OnItemDataBound"
OnUpdateCommand="grdData_OnUpdateCommand" >
<ClientSettings>
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<SelectedItemStyle BackColor="LightYellow" />
<MasterTableView Width="100%"
DataKeyNames="TID"
EditMode="EditForms"
AutoGenerateColumns="false"
InsertItemDisplay="Top"
CommandItemDisplay="Top"
InsertItemPageIndexAction="ShowItemOnFirstPage">
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="TID" UniqueName="TID"
HeaderText="TID" ReadOnly="true"></telerik:GridBoundColumn>
<telerik:GridDropDownColumn UniqueName="Employee" ListDataMember="Employee"
SortExpression="Employee" ListTextField="Employee" ListValueField="Employee"
HeaderText="Employee" DataField="Employee"
DropDownControlType="RadComboBox"
EnableEmptyListItem="true" EmptyListItemText="Make a choice" EmptyListItemValue=""
/>
<telerik:GridDropDownColumn UniqueName="Job" ListDataMember="Job"
SortExpression="Job" ListTextField="Job" ListValueField="Job"
HeaderText="Job" DataField="Job"
DropDownControlType="RadComboBox"
EnableEmptyListItem="true" EmptyListItemText="Make a choice" EmptyListItemValue=""
/>
<telerik:GridTemplateColumn UniqueName="Release"
HeaderText="Release"
HeaderStyle-HorizontalAlign="Center"
SortExpression="Release"
ItemStyle-Width="170px" HeaderStyle-Width="80px"
DataField="Release" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Release")%>
</ItemTemplate>
<telerik:EditItemTemplate>
<telerik:RadComboBox runat="server" ID="rcbRelease"
AutoPostBack="true"
EnableLoadOnDemand="true"
DataTextField="Release"
DataValueField="Release"
Text='<% #Bind("Release")%>'
EnableEmptyListItem="true"
EmptyListItemText="Make a choice"
EmptyListItemValue="NR"
OnDataBinding="rcbRelease_OnDataBinding"
></telerik:RadComboBox>
</telerik:EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="ComponentJob" UniqueName="ComponentJob" HeaderText="ComponentJob"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Operation" UniqueName="Operation" HeaderText="Operation"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Quantity" UniqueName="Quantity" HeaderText="Quantity"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Priority" UniqueName="Priority" HeaderText="Priority"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Status" UniqueName="Status" HeaderText="Status"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastEdit" UniqueName="LastEdit" HeaderText="LastEdit"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
I think your having trouble with the ComboBox because you have EnableLoadOnDemand=true but you have not subscribed to the ItemsRequested event. See Load On Demand Overview for details.
Since you mention your new to the controls, you might want to get the ComboBox LoadOnDemand working outside of the grid first.
Try removing the telerik namespace/prefix from the EditItemTemplate declaration:
Change this:
<telerik:EditItemTemplate>
<%-- RadComboBox --%>
</telerik:EditItemTemplate>
To this:
<EditItemTemplate>
<%-- RadComboBox --%>
</EditItemTemplate>
Also, please share with us which documentation were you following so that we can double check and report the documentation error.
I have a simple grid on my ASPX page and I am binding it with data coming from a select query on button click event. I am not sure how to bind columns of this table with my grid as currently I am getting 8 columns, 4 with the header given in aspx page and 4 with headers of table columns. Below is my button click event code.
protected void btnSearch_Click(object sender, EventArgs e)
{
MyBookListCont myBookListCont = new MyBookListCont();
gdvMyBooks.DataSource = myBookListCont.SearchBookDetailsCont();
gdvMyBooks.DataBind();
}
And below is aspx code of gridview.
<asp:GridView ID="gdvMyBooks" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
</asp:GridView>
Looks like a silly question, but help would be much appreciated.
This is because you have explicitly specified 4 columns in this design code :
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
Now here, as you can see, there is a HeaderText property which will override the table column name and display the text that is mentioned in here.
You have a couple of options here that you can try :
First, if you want all columns like this with custom header text, you can define all the other columns with these 4 in the same manner as well. That will display all the data and column headers as per the given format.Something like this :
<asp:GridView ID="gdvMyBooks" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
//other columns using same syntax as above.
</Columns>
</asp:GridView>
Also, if you dont want to bind all columns coming in your query, you can use this attibute in your gridview,
AutoGenerateColumns = False
And just specify the columns that you need like you have currently done.
The other thing is, if you want to straight away bind the result in your query to your GridView, then just remove those 4 BoundField statements and leave as it is. This will bind your table to GridView with the same header names as the column names.
I hope this makes things clear.
You have to set your GridView.AutoGenerateColumns to false :
<asp:GridView ID="gdvMyBooks" runat="server" AutoGenerateColumns="False">
Otherwise, it will bind all the field of your object.
I have an issue working with the telerik radgrid control. I am trying to use javascript to access a textbox when the grid is in edit mode.
My code looks like the following:
<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
AllowSorting="False" AllowAutomaticInserts="True" PageSize="10" AllowAutomaticUpdates="True"
OnItemDataBound="RadGrid1_ItemDataBound" AllowMultiRowEdit="False" AllowPaging="False"
DataSourceID="DataSource1" OnItemUpdated="RadGrid1_ItemUpdated" AllowFilteringByColumn="False"
OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" AutoGenerateColumns="false"
OnDataBound="RadGrid1_DataBound">
<PagerStyle Mode="NextPrevAndNumeric" />
<MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="Id"
DataSourceID="DataSource1" HorizontalAlign="NotSet" EditMode="EditForms">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
<ItemStyle CssClass="MyImageButton" />
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
UniqueName="DeleteColumn">
<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="Id" HeaderText="Id" Display="false" ReadOnly="true" />
<telerik:GridBoundColumn DataField="Manufacturer" HeaderText="Manufacturer" />
<telerik:GridBoundColumn DataField="Description" HeaderText="Description" />
<telerik:GridBoundColumn DataField="Configuration" HeaderText="Configuration" />
<telerik:GridDropDownColumn DataField="TypeId" HeaderText="Type" UniqueName="PartsType"
DataSourceID="PartsTypeDataSource" ListTextField="Name" ListValueField="Id" />
What I am after is that, during edit mode, when the user selects a certain value from the dropdown list 'PartsType', one of the other fields - eg 'Manufacturer' will be shown/hide.
I could get the dropdown value (in javascript) by attaching a javascript function to the dropdownlist:
function PartsTypeIndexChanged(sender, args) {
var selectedValue = args.get_item()._text;
}
I just dont know which method to use to get the 'Manufacturer' field, so I can show/hide it.
Could anyone please help?
Many thanks.
A possible approach could be to use RadControls client-side static API
$telerik.findElement(gridDOMElement, "Manufacturer");
This will help you access the column editor - I suppose it is ASP.NET TextBox rendered as input. For numeric or date columns the editor will probably be Telerik client component so you have to use findControl instead of findElement.
More info here:
http://www.telerik.com/help/aspnet-ajax/telerik-static-client-library.html
Good luck
Please check below link.
http://www.telerik.com/community/forums/aspnet-ajax/grid/set-controls-attribute-like-enable-disable-on-insert-edit-mode-using-jquery.aspx
What I want to do is just display a few specific columns from my data source on my gridview, but for some reason I'm seeing first the boundfields that I want, followed by every column in the data source.
<asp:GridView DataSourceId="dsTasks" ID="TasksGridView" runat="server">
<Columns>
<asp:BoundField DataField="field1" HeaderText="field1" />
<asp:BoundField DataField="field2" HeaderText="field2" />
<asp:BoundField DataField="field3" HeaderText="field3" />
<asp:BoundField DataField="field4" HeaderText="field4" />
<asp:BoundField DataField="field5" HeaderText="field5" />
<asp:BoundField DataField="field6" HeaderText="field6" />
<asp:BoundField DataField="field7" HeaderText="field7" />
</Columns>
</asp:GridView>
and here is my code for the datasource, just in case thats where it is
<asp:EntityDataSource ID="dsTasks" runat="server"
ConnectionString="name=Entities" DefaultContainerName="Entities"
EnableFlattening="false" EntitySetName="Tasks" ></asp:EntityDataSource>
Sorry if this is a stupid question...I'm fairly new to .NET in general (try a week)
Also, as a side question, would it be better for me to bind the data this way, or do it in the code-behind on page_load?
Set AutoGenerateColumns to false
<asp:GridView AutoGenerateColumns="false" DataSourceId="dsTasks" ...
Personally, I prefer binding from markup than from code when and where it's possible.
You might have to set autogeneratecolumn to false for the gridview
I have a list of objects called Activity and I want to display the date, type and notes for each and every one of these activities. This is the code I'm using.
<asp:GridView ID="gvTable" runat="server" AllowSorting="true" ShowHeader="true">
<Columns>
<asp:BoundField DataField="ActivityDate" HeaderText="Date"
HeaderStyle-CssClass="date" />
<asp:BoundField DataField="ActivityType" HeaderText="Type" />
<asp:BoundField DataField="ActivityNotes" HeaderText="Notes" />
</Columns>
<PagerSettings Position="Bottom" Mode="NextPrevious" PageButtonCount="5"
PreviousPageText="Older activities" NextPageText="Newer activities" />
</asp:GridView>
However, all of the attributes of each object is displayed in the header. How can I force it to display only the columns that I want to use?
gvTable.AutoGenerateColumns = false
or
<asp:GridView ID="gvTable" runat="server" AutoGenerateColumns="False" AllowSorting="true" ShowHeader="true">
should do the trick.
Set the attribute on your gridview:
AutoGenerateColumns="false"
You need to set the AutoGenerateColumns property on the grid to false.
Have you tried AutoGenerateColumns="false" in the gridview?