Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Guys I am stumped... Novice user here.. gridview edit

Status
Not open for further replies.

Help14

Technical User
Mar 8, 2014
6
0
0
CA
Hello again my friendly experts,

I have a situation whereby I have created a asp.net gridview using template fields. Now, this seems to be working perfectly. I have also added in the EditItemTemplate and ItemTemplate some dropdownlists with the SelectedValue. Something along the lines of <asp:DropDownList ID="Edit_NewDropDownList" runat="server" SelectedValue='<%# Eval("xxx") %>'.

Now what is happening is when I run the app. and click the edit button (say I have 10 rows for example), it looks to me that on the second last row and the last row, some of the field values show blank while some of the other fields have values. Also, clicking the edit button on the second last row overlaps the last row on the gridview (not sure if that has something to do with it maybe gridview formatting/size). It is not until I click cancel that I can then see the last row with populated data.

If I were a betting man, I would say that maybe I have incorrectly written the syntax for some of the controls, but this cannot be because the rest of the rows are work perfectly.

Any ideas. Hope you can visualize this.
 
Sounds like a formatting (CSS) issue. Not really sure unless I saw the grid. Perhaps your gridview is too wide for your page?
 
jbenson001,

Sorry for the delay in getting back to you as I wanted to try a few things myself first. So I added some style sheets, adjusted the paging correctly and looks like my issue is solved:)

However, keeping inline with the issues above, I now have an Gridview update issue that is a result of the drop down list:(

So with that, in my grid view I have some dropdown lists that I structured as follows:
In my .aspx page I have:

<asp:TemplateField HeaderText="New Film" SortExpression="New Film">
<EditItemTemplate>
<asp:DropDownList ID="Edit_NewDropDownList" runat="server" SelectedValue='<%# Eval("New_Film") %>'> //I have also tried Bind instead of Eval
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

In my .cs page for the update I have:
NewFilm = (((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("Edit_NewDropDownList"))).Text);

and error I get is:

NullReferenceException was unhandled by user code - Object reference not set to an instance of an object.

I am totally lost on this and your help is appreciated. Not sure why this would be null(if that is what it means).

Thanks Jbenson.

Thanks

 
It depends on where you are putting the code (what event)
Code:
NewFilm = (((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("Edit_NewDropDownList"))).Text);

Also, it looks like in code you are trying to get the selected value of the ddl, and in the edit template you are trying to set it.

So, i don't know what the flow of your page is. I assume upon editing a row, you want to set some values, like the ddl. So in the RowDataBound event of the gridview, you need to check if the row is in edit mode. If so, then do a findcontol to get the ddl. From there I would then then set the selected value of the ddl.

Make sense?
 
I am setting this in the GridView1_RowUpdating event.

Correct, that is exactly what I am doing, I am getting the selected value in the ItemTemplate(showing it in the gridview) then clicking the edit button in the gridview (that I coded) and allowing the user to change it if he/she wants to. Please keep in mind that I have about 3 drop down lists, all following the same idea (list, edit, update) amongst other control type (text box).

This is what I have in the rowdatabound:

if (e.Row.RowType == DataControlRowType.DataRow)
{
}

So currently it isn't doing anything. If you could help with some code, that might make more sense to me cause right now, I am a bit lost on the syntax.

Thanks again.

 
I don't see an <ItemTemplate> in your gridview (at least what you posted)

So I need to get more info in order to help.

1.) When the page loads, what do you want to happen? Are you setting values in the ddls? Are the values coming from a database?
2.) When the row is edited, (Edit link is clicked) what do you want to happen?
3.) When the row is edited, and the user clicks "Update", what do you want to happen.

Also, please post all the markup for your gridview.
 
Let me explain this.

1) I have a form with a bunch of controls (drop down, text box and calendar pop up (controlled by javascript) and not asp.net calendar control.
2) When the user clicks submit button after filling out the form, the value get inserted into the database (SQL Server):
Some of the code below:

protected void btn_insert_Click(object sender, EventArgs e)
{
if (CCConnection.State == ConnectionState.Closed)
{
CCConnection.Open();
}
// here i am using the store procedure named tb_gallery_insert to insert the record inside the database.
SqlCommand cmd = new SqlCommand("Insert_Form_Fields", CCConnection);
Console.WriteLine(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = CCConnection;

cmd.Parameters.AddWithValue("@TicketNumber", txt_Ticket_number.Text);
....... until all my paramters are added

3) On page load all I want to happen is show the results submitted to the database (all rows) in a grid view - to which it is doing:

protected void Page_Load(object sender, EventArgs e)
{
//here i declare the connection of the connectionstring to attach the database with the application
CCConnection.ConnectionString = ConfigurationManager.ConnectionStrings["CCConnection"].ConnectionString;
CCConnection.Open();

if (CCConnection.State == ConnectionState.Closed)
{
CCConnection.Open();
}
CCConnection.Close();

if (!IsPostBack)
{

grd_bind();
}
}

The grd_bind() function, using a stored procedure, simply select all values from the Table to display in Gridview.

4) When looking at the page/gridview the user has the option to Edit the Gridview record, or delete it.
5) When the user clicks the edit link on the gridview record, each of the fields open and become editable
6) When the user changes those values and clicks update, I want to save these updated values back to the database using a Stored Procedure. I am trying to do this in the RowUpdating, event using and Update_Procedure stored procedure...as below:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

if (CCConnection.State == ConnectionState.Closed)
{
CCConnection.Open();
}

TicketNumber = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Edit_TicketNumber"))).Text);
NewFilm= (((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("Edit_NewFilmDropDownList"))).Text);
MovieName = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Edit_MovieName"))).Text);

SqlCommand cmd = new SqlCommand("Update_Procedure", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;

cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
cmd.Parameters.Add("@TicketNum", SqlDbType.VarChar, 50).Value = TicketNumber;
cmd.Parameters.Add("@New_Film", SqlDbType.VarChar, 50).Value = New_Film;
cmd.Parameters.Add("@MovieName", SqlDbType.VarChar, 50).Value = MovieName;
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
grd_bind();

7) What I also want in the row updating is if the user clicks the edit button, but then decides he/she does not want to update anymore but rather insert a new record, when he/she clicks on anything other then the fields on the row to be editted (not just a cancel button as in comment 8), it show close the edit functionality. What is happening now is if the user changes his/her mind about editting, and for instance, clicks the next page, if I go back to the page that record is still in edit mode. I don't want that.
What I also want to add, but need some help as well, is a file upload control, next to each record in the gridview, that when the user clicks edit, they will see a new input box, with a upload button to attach word docs, excel spreadsheets extra. In the event that they want to change that after updating, they can also do it. If they do attach a word doc, in a gridview colum they see just a word icon that is clickable and can open the doc (not sure if that is possible)

7) I also have some cancel functionality on row if users changes his or her mind. He/she clicks cancel button.

8) Gridview mark up:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanging="GridView1_SelectedIndexChanging"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnSorting="GridView1_Sorting" PageSize="20" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None"
CssClass="Grid_DataTable">

<Columns>
<%--here i am using templatefields to for binding the gridview--%>

<asp:TemplateField HeaderText="ID" SortExpression="Intake" Visible="false">
<EditItemTemplate>
<asp:TextBox ID="ID" runat="server" width="30px" Text='<%# Eval("ID") %>'> </asp:TextBox>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Ticket Number" SortExpression="Ticket Number">
<EditItemTemplate>
<asp:TextBox ID="Edit_Ticket_number" runat="server" Width="100px" Text='<%# Eval("Ticket_Number") %>' ReadOnly="True" BackColor="Silver"> </asp:TextBox>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Ticket_Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="New Film" SortExpression="New Film">
<EditItemTemplate>
<asp:DropDownList ID="Edit_NewFilmDropDownList" runat="server" SelectedValue='<%# Eval("New_Film") %>' >
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("New_Film") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Movie Name" SortExpression="Movie name">

<EditItemTemplate>
<asp:TextBox ID="Edit_MovieName" runat="server" TextMode="MultiLine" Text='<%# Eval("Movie_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" width="100px" Text='<%# Eval("Movie_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete"
onclientclick="return confirm('Stop\n Are you sure you want to delete this column!')">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<EditItemTemplate>

<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false"
CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"
CommandName="Cancel">Cancel</asp:LinkButton>
</ItemTemplate>
</EditItemTemplate>

<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True"
CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false"
CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"
CommandName="Cancel">Cancel</asp:LinkButton>
</ItemTemplate>
</EditItemTemplate>

<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True"
CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

8) I don't have any page sorting yet and was trying to work on that, but need more time.

Hopefully this make sense?

Thanks SO MUCH jbenson.
 
What is happening now is if the user changes his/her mind about editting, and for instance, clicks the next page, if I go back to the page that record is still in edit mode. I don't want that.
You will have to use the PageIndexChanging event and set the edititemindex = -1 before binding the grid. (GridView1.EditIndex = -1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top