This has been asked many times, but I can't seem to find a direct answer.
Using C#, I use a datatable to fill a gridview. The first row of my datatable contains the table headers, so I loop through that row and fill the gridview header row. But now I need to get rid of that first row (the header row) in the table. I've looked all over, and all sources agree that the correct syntax is:
strSQL = "SELECT * FROM [Station ID Request$]";
OleDbDataAdapter adaBatch = new OleDbDataAdapter(strSQL, strConnExcel);
DataTable dtBatch = new DataTable();
adaBatch.Fill(dtBatch);
gv_stations.DataSource = dtBatch;
gv_stations.DataBind();
// Fill gridview headers with first row of data (spreadsheet headers)
int i = 0;
foreach (DataColumn col in dtBatch.Columns)
{
gv_stations.HeaderRow.Cells[i].Text = dtBatch.Rows[0][i].ToString();
i++;
}
// Remove the first line of the gridview (contains header info)
gv_stations.DeleteRow(0);
Everything works but the last line, which gives me this error:
System.Web.HttpException (0x80004005): The GridView 'gv_stations' fired
event RowDeleting which wasn't handled. at
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e)
at System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32
rowIndex) at System.Web.UI.WebControls.GridView.DeleteRow(Int32 rowIndex)
Every resource I've tried says that last line should be OK. Can anyone tell why I'm getting that error?
EDIT: Here is my gridview object, as requested.
<asp:GridView ID="gv_stations" runat="server"
AutoGenerateColumns="True"
CssClass="c_gvv c_gvv_stations_results"
style="white-space:nowrap;">
</asp:GridView>
Having read through your comments to other answers I'm a little confused as to what you're saying the problem is. Is it that if you simply bind to the gridview the first row in the data table (headers) appears as both the header row and the first row of data in the gridview?
It seems odd that if you don't want the header row that you first retrieve it and then go through the hassle of adding it all with your foreach loop only to then try and remove it.
If you don't want the header row selected in the first place you can amend your connection string (presumably strConnExcel?) to include "HDR=No", which will stop it from bringing back the first row in the first place.
However, if you want the header row then use "HDR=Yes" and simply bind to your gridview. It will know what the header row is. Looking at what you're doing though it looks like your trying to add the header row and then delete it!?
You are trying to delete a row from GridView rather you should delete it from DataTable. Use this line:
dtBatch.Rows[0].Delete();
Instead of this:
gv_stations.DeleteRow(0);
You can also simply your loop:
for(int i = 0; i < dtBatch.Columns.Count; i++)
gv_stations.HeaderRow.Cells[i].Text = dtBatch.Rows[0][i].ToString();
Complete code:
...
//Fill gridview headers with first row of data (spreadsheet headers)
for(int i = 0; i < dtBatch.Columns.Count; i++)
gv_stations.HeaderRow.Cells[i].Text = dtBatch.Rows[0][i].ToString();
//Remove the first line of the gridview (contains header info)
dtBatch.Rows[0].Delete();
//now do the bindings
gv_stations.DataSource = dtBatch;
gv_stations.DataBind();
You are binding the GridView to a DataSource:
gv_stations.DataSource = dtBatch;
gv_stations.DataBind();
Either remove it from the datasource and then databind, or databind and remove it from the gridview without redatabinding.
Or if you want to keep it there, You can make it invisible:
gv_stations.rows[0].visible=false
Related
So basically what I want to do is to select data from the combo boxes, and write something in the textboxes, and when I press the button all these data will fill the proper places of the datagridview.
I only found codes that fill up rows that are next to each other but I would have to write some logic for them to fill specific rows or don't fill them on certain values
private void fill_Click(object sender, EventArgs e)
{
table.Rows.Add(combo1.Text, combo2.Text, combo3.Text, text1.Text);
dataGridView1.DataSource = table;
}
This code only does the trick if these rows and values are supposed to be next to each other. But in my case, they don't.
How can I refer to a specific row that I want to fill?
It appears you are adding a "new" ROW to the table, so I am not sure what you mean by “specific” row? I assume you mean a “specific column” in a new row? If that is the case then this should work…
DataRow dr = table.NewRow();
// by column Name...
dr["Name"] = combo1.Text;
// or by index
int colIndex = 3;
dr[colIndex] = text1.Text;
// then add the row;
table.Rows.Add(dr);
I'd like to know if there is a way to automatically add columns in a DataTable inside a foreach loop? That is what I mean with "automatically".
I had in mind something like this:
DataTable dt = new DataTable();
foreach (var item in model.Statistik)
{
dt.Columns.Add();
row = dt.NewRow();
row[//Name of column goes here] = // either item or item.property;
dt.Rows.Add(row);
}
This is much more efficient than explicitly name every column before the for each loop. Because, what happens if some property changes or gets deleted, etc.? This is therefore, something I wish to get rid of.
I don't think you understand how dt.Columns and dt.Rows are related. There is one set of Columns for a DataTable. Every Row in the DataTable has all the matching columns - you don't have unique Columns in each Row. So every time you do dt.Columns.Add you are adding a column to every Row, old or new.
Also, how do you expect Row[<name of column>] to work if you don't specify the name to the DataTable?
i am adding an unbound checkbox to my gridcontrol to make multi checking to the rows like the Foto.
now i need to retrive these selected rows data in the code.
any help for code helping me get the selected rows data.
It appears you're using the GridControl's built-in checkbox selection system. In that case, you can use the GridView's GetSelectedRows method to retrieve an array of selected row handles.
int[] selectedRowHandles = gridView1.GetSelectedRows();
for (int i = 0; i < selectedRowHandles.Length; i++)
{
object row = gridView1.GetRow(i); //get a row, do something with it
}
I have a grid view table populated using a stored procedure. The grid view table loads and works fine. The problem I am having is how to get all the values in a particular column.
So far, I can only get the value of the 1 column and cell. I need all the values in that column for each row. Numbers of value varies depending on the parameters user set.
code is in c#. This gets me the first column, first row.
gridviewID.DataSource = cmd.ExecuteReader();
gridviewID.DataBind();
TxBx.Text = gridviewID.Rows[0].Cells[0].Text;
You can use for each loop to get all the rows within the grid view.
IList<string> parameters = new List<string>();
foreach (GridViewRow row in AdminSearchGridView.Rows)
{
parameters.Add(row.Cells[0].Text);
}
I have a situation where I want to make a generic codebehind function to show a message row that spans the whole table. I have previously passed the Table object and the number of columns so that it could set the column span, but this is somewhat error prone as we sometimes add new columns and I have to update the column count numbers for the messages.
There doesn't seem to be any column count in the Table object and neither any way to get the TableHeaderRow that has been added in the aspx file. I'd like to avoid having to add id's to all the TableHeaderRow's as well.
Try this, should work (assuming the TableHeaderRow is the first child of the Table):
int j = 0;
foreach (Control current in tableId.Controls[0].Controls)
{
if (current.ToString() == "System.Web.UI.WebControls.TableHeaderCell")
{
j++;
}
}