selected row to text box - c#

I try to select the row from grid and show it in text box to make update and delete to the row.
I have tried this code with DataGrid. It worked, but when I use it in infragistics webdatagrid it gives me null
protected void WebDataGrid1_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
{
txtCode.Text = WebDataGrid1.Rows[e.RowIndex].Items[1].Value.ToString();
}
There is no error massage but data does not show in text box.

try this
protected void WebDataGrid1_SelectedIndexChanged(object sender,DataGridViewCellEventArgs e)
{
txtCode.Text = WebDataGrid1.Rows[e.RowIndex].call[1].Value.ToString();
}

Related

Get the first value from a DataGridViewRow without selecting the entire row

I have a DataGridView, I want the user to be able to select any value in it and when a button is pressed it should show the first value from that row.
private void Button_Click(object sender, EventArgs e)
{
MessageBox.Show(Datagridview.SelectedRows[0].Cells[0].Value.ToString());
}
This of course gives an error seeing there is no row selected.
So how can I get the selected row from the selected cell to use in a way that I am able to get the value from the first cell of the row?
The only "solutions" I can find online involve forcing the user to be only able to select the entire row, not cell.
you can keep cell seclection and use the row of CurrentCell
private void Button_Click(object sender, EventArgs e)
{
var current = Datagridview.CurrentCell;
if (current == null) return;
MessageBox.Show(Datagridview.Rows[current.RowIndex].Cells[0].Value.ToString());
// or with indexer:
// MessageBox.Show(Datagridview[0, current.RowIndex].Value.ToString());
}

How to get selected row index in devexpress gridcontrol?

I have devexpress gridcontrol which looks like that:
I have click event on this red X button:
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
}
How can get there row index where this button is ?
You cannot access rows on GridControl, since this is just a container for the views.
As I can see from your picture you're using GridView. When you press the delete button, focused row changes and you can access it via FocusedRowHandle.
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
var gv = myGridControl.MainView as GridView;
var index = gv.FocusedRowHandle;
gv.DeleteRow(index);
}
You can use the GridView.FocusedRowHandle property:
view.DeleteRow(view.FocusedRowHandle);

Edit The listbox selected text in Runtime asp.net

I have a listbox in asp.net
I tried when user click on it's item in runtime can change the text of this item without change the order of the the items .
So I try to make Edit button when click on it get the item value
as
protected void btnEditListValue_Click(object sender, EventArgs e)
{
string listvalue = lstParameters.Items.IndexOf(lstParameters.SelectedItem).ToString();
string listText = lstParameters.SelectedItem.ToString();
}
then I will make textbox fill value from string listText to enable user to edit
what shall I do after that to keep listbox order as old without delete and insert again
please help
You can overwrite the ListItem.Text, f.e. when the user changed the text:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(lstParameters.SelectedIndex >= 0 && !String.IsNullOrWhiteSpace(TextBox1.Text))
{
ListItem selectedItem = lstParameters.Items[lstParameters.SelectedIndex];
selectedItem.Text = TextBox1.Text;
}
}

how to get the column index of the gridview

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}
It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link

How to replace text in some cells of GridView after data binding?

I have a GridView that is assosiated with database. Here's a data binding:
protected void GridViewProgramms_SelectedIndexChanged(object sender, EventArgs e)
{
int rowIndex = ((GridView) sender).SelectedIndex;
var programid = ((GridView) sender).Rows[rowIndex].Cells[1].Text;
GridViewEx.RowEditing += GridViewEx_RowEditing;
SqlDataSource1.SelectParameters["ID"].DefaultValue = programid;
GridViewEx.DataBind();
ExcersicePanel.Visible = true;
PanelAp.Visible = false;
}
Everything works fine, but I need to change some cells values in GridView after that. I need to rewrite every Cell in the last row. How to do this without affecting the database?
You will need to write your code(To change the cell text) in RowDataBound event of the gridview after data bind
Refer following link for more explanation
Update GridView Column after databinding
protected void GridViewEx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
label or textbox ddltype = (label or textbox)e.Row.FindControl("id");
ddltype.text="ur text";
}
}

Categories