Get column name dynamically in AspxGridview - c#

I am working with Aspxgridview with contrxtmenustrip in web application.
In aspxgridview there are 5 columns with some row data. Post right click on grid I need to know which row user has clicked and what is the column name without passing the column index manually in code level.
int rowVisibleIndex = int.Parse(hf["VisibleIndex"].ToString());
By doing this I am able to get the row number. In the same way I need the column name which is selected by user at client side.

gridView.Rows['Index of selected row']['Index or column name of cell you want to get value` from']
that in concrete:
gridView.Rows[gridView.SelectedRowIndex]["ID"]
UPDATE and for column Header use this :
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string headerText = dataGridView1.Columns[e.ColumnIndex].HeaderText;
}

Related

aspxgridview row command delete

I want to aspxgridview delete that row in the row command.
protected void BootstrapGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridViewRowCommandEventArgs e)
{
if (this.IsPostBack)
{
if (e.CommandArgs.CommandName == "delete")
{
ROW DELETE CODE
}
}
}
You use the ASPxGridViewRowEventArgs.KeyValue property to get the row ID and then delete the specific record from the database. You must specify the KeyFieldName="keyfieldcolumnname" to get the key column value at the RowCommand event.
The ASPxGridViewRowEventArgs.KeyValue property provides a capability to get an ID value by a key value.
Example:
<dxwgv:ASPxGridView ID="gv" runat="server" AutoGenerateColumns="False"
KeyFieldName="ID"...
protected void gv_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e) {
ASPxGridView grid = (ASPxGridView)sender;
object id = e.KeyValue;
//Delete record using data adapter or some other ORM
// Delete from users where userid=id
}
References:
Fields value in ASPxGridView on Row Command
ASPxGridView - How to implement CRUD operations with a custom data source
CRUD Operation Using Stored Procedure In ASP.NET GridView Real Time
Deleting the row from datagridview does not affect the datatable in the database. You need to write a query to remove data from the database..
If you are using sql, mysql etc, try something like getting the primary key or any unique column on the link button event and implement a command to remove the row.
something like this in your click event to get the ID to pass to database
int MyID = int.Parse(yourDataGridView.SelectedRows[0].Cells[0].Value.ToString());
and the query to delete from the database
delete from MyTable
where p_Key = MyID;

Setting the cell and row pointer in datagrid doesnt work

I have a form with a datagrid that has a list of records from the table. When the user selects one record to edit its value I want to return to the same row (record) in the datagrid and display it as selected. Of course the datagrid should show the new(edited) values.
The code in the EditButton does all of it, but the result is not what expected.
private void zButtonEdit1_Click(object sender, EventArgs e)
{
// Store the rowIndex and columnIndex values
zButtonEdit1.rowIndex = dataGridView1.CurrentRow.Index;
zButtonEdit1.cellIndex = dataGridView1.CurrentCell.ColumnIndex;
// Store the Id of the current record
zButtonEdit1._id_ = Convert.ToInt16( dataGridView1["id_lice", zButtonEdit1.rowIndex].Value.ToString());
// Open the Editform with required data for that ID
Lice frmLice = new Lice(zButtonEdit1.UIB, zButtonEdit1._id_);
frmLice.ShowDialog();
// When editing is finished, refresh the grid with new values
refresh_controls();
//Set the current record pointer (or row pointer) to the record that was edited
this.dataGridView1.Rows[zButtonEdit1.rowIndex].Cells[zButtonEdit1.cellIndex].Selected = true;
}
This is what I get Datagrid on Form
Its obvious that I changed only the selection but I have no idea how to change row (record) pointer.
As requested:
To set the current selected cell:
this.dataGridView1.CurrentCell = this.dataGridView1[YourColumn,YourRow];
Info:
https://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Select the wpf datagrid cell at runtime

My scenario:
Data loads in to my data grid page by page
On key down of the last row on w pf data grid, i will load another row in the end. this way after binding with the new rows after key down focus on the last selected cell is lost. i want to retain the selection on the last+1 (row and column) cell. How can i set this.
There is event SourceUpdated, so you could do something like that:
private void DataGrid_SourceUpdated(object sender, DataTransferEventArgs e)
{
(sender as DataGrid).SelectedIndex = (sender as DataGrid).Items.Count - 1;
}
It could help also:
How to select a row or a cell in WPF DataGrid programmatically?

Get a specific cell when selecting a row from datagrid

Im using a SQL database to store data. I have a datagrid where i show the data from the database. The problem is, when a user select a row in the datagrid, and click on my "Delete" button, i want to get the value for that cell under my column "ContactID" for that row.
I hope someone can help.
private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvProducts.SelectedCells.Count > 0) // Checking to see if any cell is selected
{
int mSelectedRowIndex = dgvProducts.SelectedCells[0].RowIndex;
DataGridViewRow mSelectedRow = dgvProducts.Rows[mSelectedRowIndex];
string mProductName = Convert.ToString(mSelectedRow.Cells[1].Value); //put your columb index where the 1 is
dgvProducts.CurrentCell.Value = null; // removes value from current cell
SomeOtherMethod(mProductName); // Passing the name to where ever you need it
UpdateDataBaseMethod(dgvProducts); // You can do that bit
}
}
Then to put that value somewhere else you could do..
private void SomeOtherMethod(string pProductName)
{
dgvProducts.Rows[dgvProducts.CurrentRow.Index].Cells["ContactID"].Value = pProductName; // where do you want it to go? Change the Column index to change the Column on the same row
}
hope this helped, although you may want to move some of it about if you want to attach it to your delete button.

How to determine which row is clicked in GridView using c#

I have a GridView1 in my form which populate a table from my Database.
The columns in my table are
ID NAME EMAIL ADDS
Now I want a user to click on ID Row and show a message in a label which row is clicked by a user. How can I do this? I've been googling for a long time.
You can find out the clicked row/column in the DataGridView.CellClick Event Handler.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// e.RowIndex
// e.ColumnIndex
}

Categories

Resources