Extract data from the last row of gridview in ASP.net - c#

I know how to to do it and put the data on textbox when I select a row from a gridview, What I want to achieve now is when I open a modal pop up form containing a gridview, it will automatically select or extract the date column from the last row of gridview.
This is for the purpose of determining the last date from the record.
Here's what I got so far (under click event for button "ADD")
if (grdSpecificTenantRental.Rows.Count == 0)
{
txtdatefrom.Text = "No record yet";
}
else
{
GridViewRow rowtwo = grdSpecificTenantRental.Rows[grdSpecificTenantRental.Rows.Count - 1];
string index = rowtwo.Cells.ToString();
txtdatefrom.Text = index;
}
Here's the output int he textbox: System.Web.UI.WebControls.TableCellCollection
Obviously this line is incorrect: string index = rowtwo.Cells.ToString();
I want to extract the 4th column in the last row which is the end date

Currently you are getting all the cells that is CellCollection so if you want to get perticular column value then use that array's index to get the required columns value.As you mentioned 4th columns so use the index 3 because index starts from 0.You might want to access it like this,
txtDateFrom.Text = grdSpecificTenantRental.Rows[grdSpecificTenantRental.Rows.Count - 1].Cells[3].Text;

Related

DataGridView Edit only 1 row in the column

I have an home work i almost finished it all but i could not get this one
this is my data grid view columns
Name , Price , Quantity , Total , button(to increase) , button (to decrease)
in the data Grid View i made a button when i double click on the increase button the quantity will increase by 1 every time
so i made this code to that
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;
}
but it do the Entire column if i have 2 rows when i double click on it the quantity of both rows is increasing
how can i change that code to just 1 row for the button
You are looping over all rows with for statement.
You should use the CurrentCell.RowIndex property to get the current selected row.
So you code should look like:
var row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;
the problem is the foreach.
in the button event you need to get the row index.
You can get the row index with
var index = datagridview.CurrentCell.RowIndex;
Than you can do something like this:
dataGridView1.Rows[index].Cells["Qty"].Value =
Convert.ToDouble(dataGridView1.Rows[index].Cells["Add"].Value)
+ Convert.ToDouble(dataGridView1.Rows[index].Cells["Qty"].Value) + 1;

Load data from gridview into a textbox

I want to load data from a row that when selected by means of a button loads them into a format. For this I am doing with the CommandName, as if they were coordinates, clicking on such a row will load this data and so on for all. But I do not know how to do it in rows and cells with textbox.
I did so like it's in the code but I do not know if it's fine.
Where you have two dashed lines is how I wrote it for the Textbox.
In the form (format) I have Textbox and there should automatically appear the data when selecting any row.
if (e.CommandName.ToString() == "clic") {
//DETERMINING THE INDEX OF THE SELECTED ROW
int indexrow = int.Parse(e.CommandArgument.ToString());
int id = (int)this.gridview.DataKeys[indexrow]["Id"];
-- TextBox1.Text = gridview.Rows[indexrow].Cells[2].ToString();
This is how you accomplish what you want (I think). But in your question I don't see a TextBox present in the GridView.
if (e.CommandName.ToString() == "clic")
{
//cast the container of the sender back to a gridviewrow
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
//get the row number
int indexrow = row.RowIndex;
//get the value of cell 2 in the textbox
TextBox1.Text = gridview.Rows[indexrow].Cells[2].Text;
}
If you mean to use "Edit" mode in the GridView with EditItemTemplate, then have a look at this tutorial.

winform multiple select grid row and get row index of selected rows

I have a databound gridview in my winform. I want to know how to get the index of Currently selected rows i.e multiple rows.
I am able to do this with a single row. but is there a way I can have a checkbox or something in which I can index of multiple rows.
The Image below will help u understand better of my requirement.
First set CellContentClick event to your DataGridView.
dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
For every cell click it will invoke the following method. Here you can create a list and populate it with clicked row index.
public void onCellContentClick(DataGridViewCellEventArgs cell)
{
// Check whether selected cell is check box column, here 0 indicates the check box column.
if (cell.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;
if(isChecked)
{
// Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
cell.RowIndex;
}
}
}
Use DataGridView.SelectedRows property to get all selected rows.
To select multiple rows:
DataGridView.MultiSelect = true;

select the id value of selected row in gridview

How can get the value of selected row in grid view without events function ?
I using something like that but it doesn't work:
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
Your problem is that you don't actually know how to select a row, you have to select a row by clicking on the row header, or if you want it simpler, just use this code:
gvMember.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
then you can just click on the row itself. That way, it will ensure that there is always at least 1 row selected. If you don't want that full row select mode, you have to check the SelectedRows.Count and notify user to select row by clicking on the row header like this:
if(gvMember.SelectedRows.Count > 0){
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
//... other code
} else {
MessageBox.Show("There is not any row selected, you select row by clicking on the row header!");
}
If you want to get the id corresponding to the current row, you can use the CurrentRow property:
int select= Convert.ToInt32(gvMember.CurrentRow.Cells[0].Value.ToString());

C# How to reposition row selection on DataGridView programmatically

I am using linq2sql to add a record to my datagridview and refreshing the grid. Right now after refresh it moves the row selection to the top. How would I tell it to highlight the most recent added record?
Not sure if this would help but I have included the code below I am using to find out the pk of the selected row. As what I would like to do is kind of the opposite. Thanks
//get selected row index
int index = this.dataGridView1.CurrentRow.Index;
//get pk of selected row using index
string cellValue = dataGridView1["pkColumn", index].Value.ToString();
//change pk string to int
int pKey = Int32.Parse(cellValue);
If you know in advance that anytime a new item is added, it should appear at the bottom of the list, you could use:
int index = this.dataGridView1.Rows.Count - 1;
this.dataGridView1.Rows[index].Selected = true;
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
But, if your order the list by other column then you're not going to get the desired results.
I recommend you, is to search for the row-index you need to give the selection to, this could be performed by a function, let's say int SearchRowIndexForPKValue(int pKey), that will perform a look-up within the cell values of the column that contains your PK/ID ("pkColumn") returning the row index. In such way the code above should looks like:
int index = SearchRowIndexForPKValue(pKey);
this.dataGridView1.Rows[index].Selected = true;
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
EDIT: int SearchRowIndexForPKValue(int pKey) is not an existing built-in method of the DataGridView. You must declare it and implement it in your code; for the body of the method, you can get the code from this post.

Categories