I am using a website and i have a gridview in that website.... I want to delete a row in that gridview by using that selected row column values and Column headers... i got the column values by using TablecellCollection but i cant get that column value's Corresponding Column header.... how shall i get that Column header by using TableCellCollection....
Please anyOne Tell me the solution of this....
Thanks in Advance!
And My code is:
int Row = Convert.ToInt16(e.RowIndex);
TableCellCollection collection = GrdViewDetails.Rows[Row].Cells;
for (int i = 0; i < Collection.Cells.Count;i++)
{
strColumnValue = Collection.Cells[i].Text;
//strColumnName=?
}
GrdViewDetails.Columns[i].HeaderText
Related
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
}
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
I got row id and column id
dataGrid2.Items[row].Cells[column].Text = "text";
row = row index;
column = column index;
you can simply update the value of the underlying datasource and its show in your datagrid. you dont need any index stuff
i use these following codes to get cell's value in datagridview.
these codes show me each cell has been clicked.
i want to use some codes just show me special column for example column with index 1 but these codes show me each which has been clicked. Imagine i just want to show column 1 with it's clicked cell. please help me to solve this
string str = dataGridView1.CurrentCell.Value.ToString();
Thanks in advance
So you're trying to get the data from the second column for every row?
int index = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[index];
// Do something.
}
Edit: Per your comment, you're trying to get the value of the second column when any cell in the row is clicked. Try this:
string str = dataGridView1[1, e.RowIndex].Value.ToString();
I am using a DataGridView to display some data in my application.
The data in the table gets changed dynamically according to the users input.
I am able to retrieve the data according to the user.
I have to add an extra column named ID and fill in the values serially starting from 1 to the number of rows which are generated dynamically.
I had added the column using dgrid.columns.add("UID");
But how to insert values at runtime?
Seeing your code, it is not correct to do:
dgrid.Columns.Add("UID");
You will have to do:
dgrid.Columns.Add("uidColumn", "UID");
To modify/add the value of an existing cell, if the row already exists, you can do:
dgrid.Rows[0].Cells["uidColumn"].Value = myValue;
That will modify the value of the column with name uidColumn and row 0. According to your problem, all you have to do is:
for (int i = 0; i < dgrid.Rows.Count; i++) {
dgrid.Rows[i].Cells["uidColumn"].Value = GetValueOfRow(i);
}
supposing that you have a method GetValueOfRow that receives a row index and returns the value you need in the ID column in that row.