I want to clear my gridview. I have 2 GridViews and Has Select Button On It. on Selecting this button that item goes into the second gridview. now the question is how should i clear the second grid view. I am trying the clear method but clear method is not found in my visual studio..
dataGridView1.DataSource = null;
or
dataGridView1.Rows.Clear();
gridview.DataSource = null;
//rebind to gridview
gridview.DataBind();
Bind the Gridview to an empty list.
Binding it to 'null' like Patrick Kafka mentioned should work - unless you have some column requirements (I'm mentioning it because I have a tendancy to plug in javascript into my gridviews and unless you specify those columns in the markup, they won't be generated and it will cause errors in the js. (This is also relevant to those getting errors after doing Columns.Clear )
In a case like that (as well as in all other cases), you can simply bind the gridview to a new instance (or empty instance) of your datasource. (Below example for a gridview bound to a datatable - it could be bound to new List<T>() as well).
grdiview1.DataSource = new DataTable();
grdiview1.DataBind();
dataGridView1.Columns.Clear(); //this clears the entire Gridview
Simply add the following c# code to clear the GridView:-
gridView.Rows.Clear();
Related
I'm having the following problem:
What I have is a search function that runs a query on my database using a SqlDataAdapter.
I then use:
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "table title");
gridView1.DataSource = ds;
gridView1.DataBind();
I originally got an error that I then had both a dataSource and dataSourceId, so I cleared the previously used ID. The code works, I can search the table and my query will return rows and update the table. The problem is that I want the data that it returns to be editable. I have my grid view editable, and before I run the query, I can edit the rows of the table. But after the search is run, and the gridView is filled with my DataSet, I'm unable to update the rows. If I click the edit button, it gives me an error saying the RowEditing event wasn't handled.
I looked into that event, and understand what's happening, if I were to program in the events for RowEditing/RowUpdating/RowUpdated etc. I could get it to work, but is there no better way to do this? Is there a simpler way to set my dataSource in C# and be able to maintain the editablity of the rows?
Thanks a lot!
I suppose part of the question would be would you want to? I guess you're looking for a SaveDataChanges() method which brings about a few design concerns. One is you have to remember that web pages are stateless so to an extent your web page post isn't aware of the data it's just pulled and bound to the UI. The grid view control in a round about way nicely gets the user to help you out here and click update on the row they've amended. With this you have your event handler and you can just focus on the data that's been changed and just send one UPDATE statement.
The problem I have is this: I have one GridView with a list objects as its data source, it binds with no problem. I have a DropDownList which have the event SelectedIndexChanged, and with this event, I add a new object to the list, after added I want to do a GridView1.Databind() so the new object appears in the GridView.
But this does not work, after I do GridView1.Databind() the data from the GridView gets lost. It even shows the EmptyDataText, but if I refresh the page, everything appears. Also, the DropDownList uses an object list as DataSource and was supposed to use DropDownList1.Databind() but it doesn't work too. It has the same data after the DataBinding, what I did to correct at least the DropDownList was to set the DropDownList again manually.DataTextField = object.attribute1 and DropDownList.DataValueField = object.attribute2. But with the GridView I'm stuck, can someone explain how the method DataBind works with a list of objects?
To refresh a asp:GridView bound to a list set gridView.DataSource property first.
gridView.DataSource = mylist;
gridView.DataBind();
For non-database data it means that list itself must be preserved either in session variable or ViewState.
I remember having this issue a long time ago. I'm guessing the issue is that you don't have the GridView in an UpdatePanel.
If this is the case, the GridView won't refresh and show the changes right away. There's some trickiness about the dropdown placement (I think it has to be in the panel, as well), but I'm sure you can figure it out with some quick searches on UpdatePanel.
I'm using a gridview and sqldatasource.
When I'm adding a new row in the gridview I need to refresh the page in order to see the new row.
The View state is enabled.
Is there anything I can do ?
Thanks
The DataBind() method of the GridView will reload the data. It is especially useful when you are changing your selectStatement of the SqlDataSource as well. It is a good habit to use this after updating anything that is contained in your GridView.
Call DataBind() on the relevant object (in your case it is probably the SqlDataSource, otherwise is generally the GridView). Doing so should bring up your new record assuming the record actually did get added (you should verify this).
Reference: Control.DataBind Method
Call DataBind() after inserting your new row.
In my website I am dynamically binding the datasource to the grid based on the value in dropdownlist.
I want to edit and update the values in grid view and the respective updations should be done in the original database.
How can I do that?
In the most basic and direct way, you use the OnUpdateCommand event of the datagrid to invoke a server site handler. That handler will receive a DataGridCommandEventArgs parameter containing an Item property which is a grid row with updated values. Retrieve key and new values from that row and build a corresponding update command.
do you know how to bind dropdownlist vai datasourase?
and witch data sourse you use tell me first.
else you just make a SELECT query and fill the dataset after that you you must have to bind dropdownlist like this....
ds = dropdownlist.DataBind();
i think this help you.......
else you can tell me if any problem occure in this code..........
I'm trying to add HyperLinkColumns dynamically to my GridView. I have the following code:
HyperLinkColumn objHC = new HyperLinkColumn();
objHC.DataNavigateUrlField = "title";
objHC.DataTextField = "Link text";
objHC.DataNavigateUrlFormatString = "id, title";
objHC.DataTextFormatString = "{2}";
GridView1.Columns.Add(objHC);
This doesn't work, so.. how can i add a HyperLinkColumn to my GridView?
You might want to add it when the row is binded:
protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLink hlControl = new HyperLink();
hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2)
hlControl.NavigateUrl = "http://www.stackoverflow.com";
e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example
}
You have to do it before the DataBinding takes place, check the GridView Events.
I think you should be using a HyperLinkField not a HyperLinkColumn.
In case, if you just want to redirect to another URL then simple use HyperLink web control and push it in the desired cell of GridView Row at RowDataBound event.
OR
If you want to perform any server event before sending it to another URL, try this
1) Add LinkButton object at RowDataBound event of GridView.
2) Set the CommandName, CommandArgument property, if requried to pass any data to this object.
3) Capture this event by handling the RowCommand event of the GridView.
By the way, I just think that you can use the DataGridView and in the Designer select the Link column and your problem would be over. The DataGridView does have a link column, than you just need to add an event on "Click" and you will be able to have what you want. This solution works if you can switch to DataGridView.
I know this thread is old but couldn't help adding my 2 cents. The procedure explained in the following tutorial worked perfectly for me:
ASP Alliance
It seems you have got things mixed up. I don't know - how that code compiles?
GridView's column collection can accept columns of type "DataControlField".
I think you need to initialize HyperLinkField and set relevant properties (text, NavigateUrl, HeaderText, Target) and add it to the columns collection.
HyperLinkColumn class is meaningful when you are using DataGrid (not in case of GridView).
Hope that helps.