How to get text boxes for Gridview Row values? - c#

Using C# & Mysql
In my webpage am using gridview, if i click the column in the girdview the value should display in the textbox.
For Example
Griview
Column1 column2 Column3
1 Raja 9876
2 Ravi 7890
3 Ramu 9879
...
If i click the 2 rows all the values should display in the textbox
Textbox1.text = 2
textbox2.text = Ravi
textbox3.text = 9879
...,
How to write a code for this condition.
Need C# code Help

I'm assuming that by stating "[...]click the 2 rows[...]" you actually mean "click the 2nd row"; at least, this is what your last snippet suggests, since it shows only the values of the 2nd row (on a little side note: the ID's wrong there; it should be 7890).
The following code snippet shows a GridView which allows the selection of a single row, and uses an event handler in the code-behind to set the text of each TextBox to the according value in the selected row:
Page.aspx:
<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
Event handler in the code-behind file Page.aspx.cs:
void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
var grid = sender as GridView;
if (grid == null) return;
//Cell[0] will be the cell with the select button; we don't need that one
Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
}

You can use an EditItemTemplate for this.
See the CodeProject article, Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List.

Related

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.

fill textbox on datagrid row click

I want to fill textboxes when a user clicks on a row. I don't want to use grdUser..SelectedRow.Cells[1].Text because my datagrid has a summary of the user details e.g. name, SSNumber, email. if I click on the row then I want all the user details should load into textboxes. How would I do that?
I would like to use a sql query but then my where clause should use SSNumber but not sure that's best practise.
C# or vb help welcome.
You can add Attributes to the Row created and when the user clicks that row redirect them to a page with the Id of the object you want to display. I know you are using a datagrid my example is using a GridView
For example on Row created
protected void ListGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType .DataRow)
{
e.Row.Attributes[ "onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';" ;
e.Row.Attributes[ "onmouseout"] = "this.style.textDecoration='none';" ;
e.Row.ToolTip = "Click to select row";
e.Row.Attributes[ "onclick"] = "Javascript:window.location = '" + ResolveClientUrl("~/PickList/ScanPickListItem.aspx" ) + "?PLRID=" + DataBinder.Eval(e.Row.DataItem, "ScreenRowId" ) + "'";
}
}
First, get the index of the selected row then get the value of the specific cell
Dim dgv_active_Row as Integer= dgv.CurrentRow.Index
TextBox1.Text = dgv.Item(1, dgv_active_Row).Value
'1' is the index of the specific column where you want to get the value of the textbox

Pass selected row in gridview to drop down list box

I am trying to pass a selected row in gridview to my dropdown. I have 1 gridview, 1 textbox and 1 dropdown list box. My dropdown is populated by SqlDataSource from my table Country.
On my gridview, I have 3 columns, which are SELECT, NAME and COUNTRY. Under SELECT column, I have my hyperlinked Select, every time I clicked on a certain row NAME will pass in textbox (no problem with that), but in my dropdown it does not populates the COUNTRY that I choose.
For example, I have Jack for name and USA for country. Beside Jack and USA, I have a hyperlink Select, when I click Select, Jack will display on my textbox and supposed to be USA will display on my dropdown but not displaying instead it says an error:
'cboCountry' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
I have tried the code below:
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
txtname.Text = GridView2.SelectedRow.Cells[1].Text;
cboCountry.Text = GridView2.SelectedRow.Cells[2].Text;
}
Only the textbox is working, no luck at all in dropdown. I'm using C# and asp.net.
Try to use cboCountry.SelectedItem.Text.After getting the value set this thing to dropdown like
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
txtname.Text = GridView2.SelectedRow.Cells[1].Text;
cboCountry.SelectedItem.Text = GridView2.SelectedRow.Cells[2].Text;
}
Hope it works

c# DataGridView getting contents from a Row/Column

I have a DataGridView with 4 columns.
When a user double clicks the row, I want to revtrieve data from the 1st column of the clicked row.
private void ticketsDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Row " + e.RowIndex); // gets me the row selected
}
How can I get the column for the selected row?
Look at DataGridView.Rows Property
try this on doublic click event.
DataGridViewRow row =dataGridView.Rows[0];
string someStringColumnValue = (string)row.Cells[0].Value;
Ref: C# - Lambda syntax for looping over DataGridView.Rows

On edit event how insert a value into the database in the same row as in the gridview?

I have a gridview.
Here are my columns:
The column choose's control is a dropdown with 2 values: yes/no
No Name Choose
1 Meh Yes/No
On edit event I want to insert the selected value of the dropdown in the database of the current edited cell.
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = ((GridView)sender).Rows[e.RowIndex];
DropDownList ddl = (DropDownList)row.FindControl("DdlChoose");
bool yes = ddl.SelectedValue == "Yes";
SaveData(params); // pseudo-code
}
1st thing first, i'd suggest data binding, and apply changes to the Database as bulks and not one at a time, unless forced to by definition.
2nd, each row have a key, then it is no problem to execute a Update the Row by
UPDATE mytable SET choose=new_value WHERE key_column=rowkey.
EDIT :
DatagridView Binding to SQL Database example can be found : http://csharp.net-informations.com/datagridview/csharp-datagridview-sql-server.htm

Categories