Binding DataTable to DetailsView control - c#

I have a DetailsView control that I want to populate with a data table. I'm doing this in code behind. After checking that the data table is created successfully, the code is:
detailsView.DataSource = dataTable;
detailsView.DataBind();
I run the program and the detailsView doesn't show.Could the problem be that a data table potentially can have multiple rows and a details view only one? If that's the problem is there a way of getting around it? I made sure in creating the data table that it has only one row. I also tried binding only one row in the data table but a data table row isn't accepted as a data source. What's the problem?

You have to add in a Bound field or Set the DetailsView AutoGenerateRows="true" to display the field in the detailsView. I am sure you forget to do this..
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" >
<Fields>
<asp:BoundField DataField="FieldName" HeaderText="Field Title" />
.......................
.......................
</Fields>
</asp:DetailsView>

DetailView.DataSource = DataSetName;
DetailView.DataMember = DataTableName;
http://www.dotnetspider.com/forum/319762-Datasource-Datamember-In-GridView.aspx

Related

Grid1 does not exist error when loading xml to html table asp.net

I want to load an XML data to a table in ASP.NET. I tried adding the following code in page_load
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("xmlName.xml"));
Grid1.DataSource = ds;
Grid1.DataBind();
But it is showing an error
Grid1 is does not exist in the current context
What could be the issue?
Verify that Grid1 is defined in your markup, like this:
<asp:GridView id="Grid1" runat="server">
<Columns>
...
</Columns>
</asp:GridView>
Note: If you don't mind that the column names will match the columns in the data set, then you can use AutoGenerateColumns="true" in your markup and omit the .DataBind() call, like this:
<asp:GridView id="Grid1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
Now you can omit the Columns definitions in the grid view markup and also remove the .DataBind() call so you only need to set the DataSource property.
make Grid1 property runat='server'
Grid1 should have the attribute - runat="server" for you to be able to access it from your code behind
Note:
It's only effective when all the root to this element has this attribute.

Adding column to a gridview

I currently have a gridview that is set up and working properly but I ran into a little bit of an issue. My grid's datasource is being set from a DB table and one of the columns in the table is the numeric ID of another table. Rather than displaying this numeric ID in the grid, I need to display a description column off of the other table. Can I add an additional column to my current gridview just for display purposes?
Yes you can accomplish this by using a template on your ID field rather than creating a new field. This link has some examples. This way you can custom format your existing column.
You can use a data-set and bind this dataset to the gridview.
Using the data-set, you can add rows, columns. The below example is a good one to add rows/columns to the grid-view. You can also follow the same example with a little bit of tweaks.
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
Set AutoGenerateColumns to false, and define the columns that you want to display:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" CellSpacing="0" AutoGenerateColumns="false" Width="100%">
<RowStyle BackColor="#ffffff" />
<AlternatingRowStyle BackColor="#f5f5dc" />
<HeaderStyle BackColor="Beige" ForeColor="#333333" Font-Bold="true" Height="25px" />
<Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%# Eval("Address") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The change should be made in 3 locations:
The datasource should have the desc column brought back with it. Do the join in the database and bring it all back at once so the front end will not need to make an insane amount of calls.
On display, you can set the id column to invisible. This can be done on the code behind or on the aspx.
On edit, if it is a value that is based off of a look-up table, you will need to create a drop down list. You can query for more info if edit is needed, if not, the top two points should cover your needs.

problem with the nested gridview

I'm trying add a gridview dynamically (lets call this gridview2) to existing gridview (lets call this gridview1) added through .aspx page.
What i'm trying to do is: Depending upon the content of the row (more specifically invoice number displayed in the cell-1 with zero based indexing) displayed in "gridview1" i'm filtering data from a list and displaying the data in "gridview2".
I'm doing some terrible mistake somewhere or my approach should be fundamentally wrong.
Below is the code for gridview1 added in .aspx page.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="ClickingBtn_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Date Of Transaction"
HeaderText="Date Of Transaction" SortExpression="Date Of Transaction" />
<asp:BoundField DataField="Invoice Number" HeaderText="Invoice Number"
SortExpression="Invoice Number" />
<asp:BoundField DataField="totalAmount" HeaderText="totalAmount"
ReadOnly="True" SortExpression="totalAmount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ComponentDBConnectionString %>"
SelectCommand="SelectUserPreviousHistory" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="duran" Name="userName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is fetching the data as shown here: here
And then I'm trying to add another gridview depending upon the unique invoice number displayed in each row by following code:
foreach (GridViewRow gridviewrow in GridView1.Rows)
{
GridView gridView2 = new GridView();
gridView2.AutoGenerateColumns = true;
String x = gridviewrow.Cells[1].Text; //IT FETCHES THE INVOICE NUMBER FROM EACH ROW
softwareTitlesList = SoftwareListRetrieve(); //lIST OF ALL THE SOFTWARE TITLES ADDED TO LIST
ArrayList titles = new ArrayList();
foreach (SoftwareTitles softwareTitle in softwareTitlesList)
{
if (softwareTitle.InvoiceNumber.Contains(x))
titles.Add(softwareTitle.SoftwareTitle); //ADDING ONLY THOSE TITLES THAT MATCH THAT PARTICULAR INVOICE NUMBER
}
gridView2.DataSource = titles;
gridView2.DataBind();
}
But nothing seems to be happening. What is wrong with this ?
Please help me
BTW I'm using asp.net/c# visualstudio 2010. And i'm not using LINQ in my project and the database is sql server 2005
Thanks in anticipation
There are quite a few problems with your approach.
You do create a new gridview in your server side code but you never add the gridview to the page or any other control on the page! It is effectively telling the .NET to create a gridview, bind it to the data but it is not telling .NET "where" to render the gridview on the page. Hence you don't see anything.
Dynamic controls - I am sorry if I am being presumptuous here but I think you haven't had much play with dynamic controls within ASP.NET. My whole hearted advice will be to not use them as far as you can. Dynamically created controls come with a HUGE baggage with respect to their state, their post back events and actually have to be added on every post back to the page. In a nutshell, if you add a button dynamically on the page as
Button btn = new Button();
btn.Text = "hi";
Page.Controls.Add(btn);
Then you must fire this code on every postback of the page. If during any postback this code doesn't get fired, the button will be missing when the client sees the page.
I can suggest some approaches to achieve what you are trying to do, however to be able to do that I would like to know
Where do you want the second gridview to appear in the page (i.e. embedded within the first one or outside of it)
When do you want to populate the second gridview? Is it something like the situation that the user clicks one row in the first gridview and then sees the relevant information in the second gridview?

Grid View with dictionary as datasource

Please help me out... I need to set a dictionary as datasource to a GridView. The dictionary contains objects with for example the attributes start and finish. I have only found information about connecting datasets, resultsets, sql adapters and so on to GridViews.
The GridView im trying to present my dictionary with, should also be editable, but I guess the NO1 issue is to just connect it.
If you just set it as the datasource and set autegenreate columns to true of the gridview it will work just fine. If you want to bind the columns explicitly then you bind them to the Key and Value properties. e.g.:
<asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Key" />
<asp:BoundField DataField="Value" />
</Columns>
</asp:GridView>
Just set the Datasource property to your Dictionary object.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx#binding_to_data

Set DetailsView as selected row of GridView

I am creating a GridView/DetailsView page. I have a grid that displays a bunch of rows, when a row is selected it uses a DetailsView to allow for Insert/Update.
My question is what is the best way to link these? I do not want to reach out to the web service again, all the data i need is in the selected grid view row. I basically have 2 separate data sources that share the same "DataObjectTypeName", the first data source retrieves the data, and the other to do the CRUD.
What is the best way to transfer the Selected Grid View row to the Details View? Am I going to have to manualy handle the Insert/Update events and call the data source myself?
Is there no way to link these two so they use the same data source ?
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsSearchData" >
<Columns>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
....Code...
<asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
Visible="false" Width="100%">
<Fields>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
...
The standard way to do it would be to have the selected item of the griview be a control parameter to the objectdatasource you have wired up to the detailsview. I would probably not worry too much about the overhead of retreiving data that you already have unless you are catering to users with such slow connections that you want to avoid roundtrips to the webserver at all costs.
If you really want to avoid that thenyou could pull the data out of the gridview using javascript/jquery and then do your inserts/updates via ajax calls. It would require lots more coding though.
This is a really old thread, but in case anyone came here looking for an answer like I did, a simple solution is to add this function to your code:
(Note that this only works if the rows in your GridView match the entries in your DetailsView.)
protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}
And modify the GridView and DetailsView to include these settings:
<asp:GridView ... OnSelectedIndexChanged="GridView1_OnSelectedIndexChanged" ... >
<asp:DetailsView ... AllowPaging="True" ... >
This will make the selected page in the DetailsView match the selected index in the GridView.
You can hide the paging options in the DetailsView properties if you dont want users to navigate using paging in the DetailsView.

Categories

Resources