How to get value from selected checkbox in ASP gridview? - c#

I have a grid view with number of pages.using check box i like to get value of selected check boxes. i have no idea to get value from selected check boxes in different pages.Please give me your suggestion and coding for this.

Use this one
protected void btnClick_Click(object sender, EventArgs e)
{
StringBuilder sbQuery = new StringBuilder();
bool flag = false;
foreach (GridViewRow row in gridview1.Rows)
{
if (((CheckBox)row.FindControl("chk")).Checked)
{
flag = true;
//------
}
}
}

As you have illustarted in question i am assuming that you want checkboxes values so i have one linke which i providing you
this will help you to understand how would you achieve and also i am recommanding you to use javascript
Get the value of checked checkbox?
this is javascript i have other links of gridview also
http://forums.asp.net/t/1125079.aspx
How to get checkbox value from gridview when checkbox OnCheckedChanged
regards...:)

Related

how to get dropdown value inside gridview on button click in c#?

I have tried this but its not working,giving null value:please tell me what have to change in this code.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
use
DropDownList ddl= (DropDownList)(row.FindControl("ddlCol1"));
and try and let me know
try this code on click event
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
DropDownList ddl1 = row.FindControl("ddlTest") as DropDownList;
if (ddl1 != null)
{
// your code here
}
}
#creby i saw your code.
you are adding your dropdown in grid view during row data bound event ok.. but now when you click on the button, all the drop down will be vanished so that's why you will not able to get drop down.
use item template of grid view and then bind dropdown in row data bound event.

how to get value of row in gridview and display in textbox?

How to get value of row in Gridview and display in Textbox?
Don't work this code. And I don't want to use this code:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
LinkButton lnkButton = sender as LinkButton;
GridViewRow row = (GridViewRow) lnkButton.NamingContainer;
lblSender.Text = row.Cells[2].Text;
lblSubject.Text = row.Cells[5].Text;
txtReadMsg.Text = row.Cells[6].Text;
Are you sure your GridView's cells don't have controls inside it.
maybe this helps you
To get the row, do:
var row = GridView1.Rows[e.NewSelectedIndex];
And then the rest of the code should work for you. You can then remove the first two lines. Also, note Text only works if you are using BoundColumn column definitions; it won't work if you are using a different type of column.
If you are getting a different error, then it's related to something else; please add a comment and we can help work through it.

C# Datagridview - Check Row is Selected

I have this code in my C# program, but it throws a fit when some buttons are clicked because there is no row selected in the DataGridView (I use the ClearSelection method):
string selectedUser = usersGrid.SelectedRows[0].Cells[1].Value.ToString();
Is there some sort of check I can do before the above line to ensure that a row is selected?
if (usersGrid.SelectedRows.Count > 0)
I am going to take a stab at what I think you are trying to do, try this below
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in usersGrid.Rows)
{
if (this.usersGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string selectedUser = this.usersGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
also do the following as well and checkout the link
Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. This will make it so the user can only select a single row at a time.

How to obtain the value of HiddenField in a GridView when a CheckBox is checked?

I have a GridView which has multiple rows, on each row I have a CheckBox and a HiddenField. On button click I want to check if the CheckBox is checked and if it is I want to take the value of the HiddenField for that row. Each HiddenField on each row has a different value. User could check multiple CheckBoxes so I need to be able to pull the value of each HiddenField.
Any help will be really appreciate it.
Thank you
Loop through each row in the grid, check if the checkbox is checked and if it is, grab the value of the hidden field.
foreach (GridViewRow row in grdView.Rows)
{
if((row.FindControl("chkBoxId") as CheckBox).Checked)
{
string hiddenFieldValue = (row.FindControl("hiddenFieldId") as HiddenField).Value;
}
}
Where chkBoxId is the ID property of your checkbox on the page and hiddenFieldId is the ID of the hiddenfield control on your page.
Possible duplicates.
How to get values of CheckBoxes inside a gridview that are checked using asp .net
Get the id of selected checkboxes in gridview (Asp.net) c#
How to get the value in the gridview which the checkbox is checked?
One of the answer in above links :
foreach(Gridviewrow gvr in Gridview1.Rows)
{
if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
{
//Get hidden field value here.
}
}
You can use a code like this:
protected void BtnMybutton_click( Object sender, EventArgs e)
{
Button Mybutton = (Button) sender;
GridViewRow row = (GridViewRow) MyButton.NamingContainer;
CheckBox ChkTest = (CheckBox) row.FindControl("ChkTest");
HidenFiekd HdfValue = (HidenField) row.FindControl("HdfValue");
if(ChkTest.Checked)
{
Console.WriteLine(HdfValues.Value);
}
}

how to check whether gridview's row is selected or not in c#.net windows application

I want to know how to check whether a gridview's row got selected or not.
I am working on windows application.
I want to put a if condition ie if a particular row gets selected then fill the textbox with the correspoding cell value.
I am just not getting the way how to give the condition in the if clause.
Handle the DataGridView.SelectionChanged event. Use the DataGridView.SelectedRows property to get the selected rows collection.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
// Update the text of TextBox controls.
textBox1.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
....
}
Check DataGridViewRow.Selected property.
if (dataGridView.Rows[rowIndex].Selected)
{
// Do something ..
}
Check the selected property of DataGridViewRow, it returns true for selected else false.
bool isSelected = dataGridView1.Rows[e.RowIndex].Selected;
You can subscribe to the SelectionChanged event of the control and iterate through each selected row if multi-selection is enabled or just the first one if single-row selection only.
private void MyGridView_SelectionChanged(object sender, EventArgs e)
{
for (int i = 0; i < MyGridView.SelectedRows.Count; i++)
{
MyTextBox.Text = MyGridView.SelectedRows[i].Cells[0].Value.ToString(); //assuming column 0 is the cell you're looking for
// do your other stuff
}
}
More information can be found on the SelectedRows property.

Categories