I have a gridview with empty cells. Whenever a cell is clicked I replace the contents of the cell (innerHTML property) with a string, using javascript.
I would like to save this changes on a 2d array when the index of my combobox is changed. However when I traverse the gridview during my selectedindexchanged event, none of the changes I did to the cells are visible (all the cells are empty). I guess the changes are not persistent.
How could I do this?
No, the changes are not persistent. You should do some reading about how forms on the web work--not just specific to asp.net--to get a fuller understanding. Basically, your SelectedIndexChanged event is really a POST of a form on your page. Only form values, like those in <input> or <select> fields will be sent to the server and be available to process in your C# code. So, one option would be to have a hidden input for every cell in your GridView. Another one would be to have a single hidden input which stores a string representation of a 2d array, and you would manipulate that with JavaScript every time you change the contents of a cell. Then, when you process this data in your C# code, you'll need to process the hidden inputs, not the cells of the GridView.
Related
I have a datagridview which displays data from database. I have added a a textbox as a searchbox and added a text changed event. In that event I have written Search query with 'LIKE' based on text in textbox. So when I type a character it will instantly search the database and display in datagridview.
But my problem is, for large amount of data, for example a million rows, this text changed event hangs the datagridview. cant display data to datagridview fastly. Any way to speed up the process?
I would use a BackgroundWorker to search my data and handle result only if the box content didn't change in the mean time. I would also add a delay on search to avoid useless search. Actually, when needed this kind of search i prefer handle keydown on Enter or/and a button to be sure that we search when we want to.
I have a C# WPF Application where i use a DataTable as Source for a DataGrid.
Now when The User moves a Column in the DataGrid(I already found a ColumnReordered Event)
I want to change the index of the Column to the Position,
that the User moved it to(0 for the first position, 1 for the second...)
I want to do that because i want to save the column positions for the next time the User runs the Apllication.
Is there any possible way to do that?
All you need to do is whenever user is about to close the form you get the column name and its respective index and save that to any format like xml, csv. Next time when user loads you can apply the re-oredering after reading his last specified settings from the file.
You can have a look at this article for what i mean. This implements the same idea. You can modify it for your WPF application though.
http://www.codeproject.com/Articles/37087/DataGridView-that-Saves-Column-Order-Width-and-Vis
Use the ColumnIndex
int columnIndex = dataGridView.CurrentCell.ColumnIndex;
Post that is already on here should help;
Get current cell column index in DataGridView CurrentCellChanged Event
Is there any way to refreshing a cell of a grid view without refreshing the entire grid view in c# asp.net
you could possibly use a templatefield, put an UpdatePanel in it and refresh that single cell that way. You would just need someway to initiate the refresh.
You may have to use a nested UpdatePanel within the cell.
If you don't want to put an UpdatePanel in every cell in your GridView, you can refresh a cell on a regular, full postback. The contents of the grid will be roundtripped, but your data source won't necessarily be queried in full.
First, make sure your grid doesn't rebind on every postback, e.g. wrap your gv.DataSource = x; gv.DataBind(); inside an if (!this.IsPostBack).
Then you should be able to do something along the lines of
((Label)gv.Rows[x].Cells[y].FindControl('myLabel')).Text =
GetDataItemNumber(x).FieldForColumnY.ToString();
Bear in mind that gv.Rows[x] may not correspond to the xth item in your data source, as Rows includes header rows etc. You might need to iterate through Rows checking e.g. IDs to find the correct row.
Disclaimer: I've never actually done this before, but it should be possible...
This will not be a very clear explanation of my problem, but I don't know how to explain it better.
I have a gridview which I create dynamically on PreInit. This gridview has textboxes dynamically added on each row.
Everytime I push the button, I loop inside the gridview cells and get the Text of the textboxes -and update the database.
the first time the gridview is created, row uniqueID's are like this:
ctl03, ctl04, ctl05, ctl06 (thus, the textbox ID's are ctl03$txt0 etc..)
The first time I push the button, the row UniqueID's are still the same, so that I can find the controls by FindControl(ID) method, or using Request.Form[txt.UniqueID]
However; after the first time, whenever I push the button, the row ClientId's are created like the following: ctl02, ctl03, ctl04, ctl05.. So that I cannot find the Textboxes and cannot catch the Text written on them.
When I look at the rendered HTML code, I see that the rowClientID's are still the same with the first created ones (ctl03, ctl04, ctl05, ctl06)
Does anyone have any idea why the rowIDs (naming container IDs) change after the first update?
Thanks in advance.
One solution is to create your textboxes with static ids and not let it dynamic create by asp.net. This can be done if you use asp.net ver 4.
Other solution is to just render a simple <input name="KnowName01" id="KnowId01" type="text" value="your value here" maxlength="100" etc... >
and then on post back you just capture the return with the old way and get the value.
Request.Form["KnowName01"]
At the end, the TextBox is nothing more than a render of the input, plus some checks for what write on it, including Anti-XSS safe.
Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.