|
Hi all,
I'm developing an ASP.Net app with C#. I have a Gridview that I want to show right on page load. I'm then capturing data into the textboxes and need to save it on the gridview right after clicking ADD button.
I can't get my Gridview displayed on page load and when saving the data from the textboxes to the Gridview I get this error:
"Column ‘Full Names’ does not belong to table"
This is what I've got in my code
html Design:
<asp:gridview id="dgInfoProfile1" runat="server" autogeneratecolumns="False">
<columns>
<asp:templatefield headertext="Full Names">
<asp:templatefield headertext="Identifying Number">
<asp:templatefield headertext="Passport Number">
Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Full Names"));
dt.Columns.Add(new DataColumn("Identifying Number"));
dt.Columns.Add(new DataColumn("Passport Number"));
this.dgInfoProfile.DataSource = dt;
dgInfoProfile.DataBind();
}
ADD button:
protected void brtAdd_Click(object sender, EventArgs e)
{
try
{
DataRow dr = dt.NewRow();
dr["Full Names"] = txtFirstName3.Text;
dr["Identifying Number"] = txtIdentityNo.Text;
dr["Passport Number"] = txtPassportNo.Text;
dt.Rows.Add(dr);
//Bind GridView Here with this Table
this.dgInfoProfile.DataSource = dt;
dgInfoProfile.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
I'm not sure where I've missed it, your advice and help in this is highly appreciated.
Thanks.
|
|
|
|
|
Welcome to correct forum.
Nopo wrote: DataRow dr = dt.NewRow();
How come this line is not showing any error.
You have to put
Nopo wrote: DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Full Names"));
dt.Columns.Add(new DataColumn("Identifying Number"));
dt.Columns.Add(new DataColumn("Passport Number"));
in ADD button click.
Just leave GridView.DataBind() in PageLoad.
|
|
|
|
|
I have put:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Full Names"));
dt.Columns.Add(new DataColumn("Identifying Number"));
dt.Columns.Add(new DataColumn("Passport Number"));
under ADD button click and now my ADD button click looks like:
DataTable dt = new DataTable();
DataRow dr = new DataRow();
dt.Columns.Add(new DataColumn("Full Names"));
dt.Columns.Add(new DataColumn("Identifying Number"));
dt.Columns.Add(new DataColumn("Passport Number"));
dr["Full Names"] = txtFirstName3.Text;
dr["Identifying Number"] = txtIdentityNo.Text;
dr["Passport Number"] = txtPassportNo.Text;
dt.Rows.Add(dr);
//Bind GridView Here with this Table
this.dgInfoProfile1.DataSource = dt;
dgInfoProfile1.DataBind();
Now, the app is complaining with this line:
DataRow dr = new DataRow();
error: 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level
How am I supposed to define the DataRow?
And as for the gridview on PageLoad, it's not showing still.
|
|
|
|
|
Nopo wrote: DataRow dr = new DataRow();
Why are you doinh this.
1. Create DataTable.
2. Assign Columns to it.
3. Create a DataRow by dt.NewRow().
4. Fill that row and add it to datatable.
|
|
|
|
|
Thanks. Here it is now:
//Create DataTable.
DataTable dt = new DataTable();
//Assign Columns to it.
dt.Columns.Add(new DataColumn("Full Names"));
dt.Columns.Add(new DataColumn("Identifying Number"));
dt.Columns.Add(new DataColumn("Passport Number"));
//Create a DataRow by dt.NewRow().
DataRow dr = dt.NewRow();
//Fill that row and add it to datatable.
dr["Full Names"] = txtFirstName3.Text;
dr["Identifying Number"] = txtIdentityNo.Text;
dr["Passport Number"] = txtPassportNo.Text;
dt.Rows.Add(dr);
//Bind GridView Here with this Table
this.dgInfoProfile.DataSource = dt;
dgInfoProfile.DataBind();
I'm making a huge progress now.
After clicking ADD button, the gridview is showing with all the column names but the data captured from the textboxes is not being saved to the gridview.
I'll double check and see if there're any typing errors.
|
|
|
|
|
sure, tell me if it works.
|
|
|
|
|
Its now working perfect. I think the problem was that I turned the AutoGenerateColumns to "false" because after changing it to "true" it worked.
Thanks a lot for all the guidance you've given me.
|
|
|
|
|
If his answer helped you then please vote/rate the messages which helped you.
Thanks !
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Great.
|
|
|
|
|
The answer is gr8 been struggle the whole day..thanx
|
|
|
|
|
Forgive me for being confused here, but this actually won't work if you want to keep adding to the datatable based on multiple selections from the textboxes, listboxes and dropdownboxes - right? This will only populate the datagrid once and then re-populate it on each click of the Add button?
|
|
|
|
|
Hi,
I'm looking for a control that does exactly the same as the control used on The Code Project for setting newsletter topics (see http://www.codeproject.com/script/Membership/Modify.aspx tab "Newsletters").
The control, when not selected, shows a comma separated list of newsletter topics. When you click the control it comes up with 4 columns of checkboxes in which you can select and deselect the newsletter topics you're interested in.
Any clues?
|
|
|
|
|
This is Nothing but a DIV . Place all the control inside DIV. Hide/Show Div on the click on TextBox
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Thank you.
You wouldn't happen to have some html / ASP.NET markup would you (especially of the controls used within the DIV)?
I guess the "Hide/Show Div on the click on TextBox" has to be done with Javascript (to prevent a postback)? A small sample script would help a lot.
Thanks for your help.
|
|
|
|
|
Gebbetje wrote:
I guess the "Hide/Show Div on the click on TextBox"
You can do it using simple JavaScript.
Here is the sample javascript :
var counter =0;
function ShowHideDiv()
{
var DivID=document.getElementById('MyDiv');
if(counter == 0)
{
DivID.style.display ="Block";
counter=1;
}
else
{
DivID.style.display ="none";
counter=0;
}
return false;
}
Note: You can also do it without using counter, just check div visibility when you are checking the counter.
Hope this will help you
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
I have a treeview in my form. I need to get the nodes count of the treeview using javascript. I know it is possible in code behind. But I want to do it in javascript.
I used the following code,but getting an error that "controlid.Nodes.length" is null or not an object.
var controlid = document.getElementById('ctl00_MainContent_CreateWizard_UnitsTreeView');
var nodecount = controlid.Nodes.length;
alert(nodecount);
Any idea how to get the count of nodes? Please help.
Thanks,
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
Why do you require this using javascript.
treeview renders nested tables. So you need to use javascript to count tables.
why dont you count in the server side and store in an hidden field ?
|
|
|
|
|
The treeview is rendered as a table, so there is no easy way to get a count of nodes - the nodes are actually table rows and cells.
You could apply a css class or other attribute to the nodes in the code behind when you are creating your treeview - then use javascript to count the number of elements with that class - very simple if you use JQuery.
Are you adding or removing nodes dynamically with javascript? If not then you could just count them as they are added server-side and embed them in your javascript when the page is rendered. This[^] post will show you how to embed asp.net variable.
Hope that helps.
|
|
|
|
|
hii
all.i have stored images in database by giving their path.now i want to show the images in datalist.
tell me how can i do this......plz guide....
thnks in advc....
|
|
|
|
|
You have to read that image in a byte array.
I suggest you to search on google, you'll find many helpful articles.
|
|
|
|
|
No, he doesn't. He's stored a path, he can just pass that path to an img tag.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Oh i am sorry, i didn't noticed that.
|
|
|
|
|
Here is one of my article which showing image from database to gridview.
Displaying Image in Gridview from Database[^]
Hope, you can also do the same for datalist.
Thanks !
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
u just give the path of the image where u need to display
|
|
|
|
|
Hello,
I suggest you use "Asp:Repeater". Put img in the container and then bind the img source to your database.
|
|
|
|