Check the current Row is a data row or not - c#

I have a gridview with 10 rows i am displaying 6 rows in each page i have a text box and and image button in each row when i click the image button all the functionalities are working but when i click the page index it is displaying an error in row command how can i check whether the row type is data row or not in gridview row command event. the code that i am using is as follows
protected void gvgridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
TextBox txtgvGroupName = (TextBox)gvRow.FindControl("txtgvGroupName");
ImageButton imgbtn = (ImageButton)gvRow.FindControl("imgbtn");
if (e.CommandName == "Edit")
{
imgbtn.Visible = false;
}
}

If(e.Row.RowType == DataControlRowType.DataRow)
then write your condition.

a little to do with the datarow here, instead you need to check if (e.CommandSource is ImageButton) at the first line of your gvgridview1_RowCommand

Have you tried checking the RowType property of the GridViewRow instance?

try this assuming you are reffering System.Data.DataRow
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
DataRow drow = gvRow.DataItem as DataRow
if(drow!=null)
{
// row is DataRow
}

Check e.Row.RowType, it allows you to compare the RowType to the DataControlRowType enum.
[EDIT] I wonder if it's something to do with how you're getting hold of the row. The code I just tried is:
GridViewRow row = Gridview.Rows[int.Parse(e.CommandArgument.ToString())];
After that I can use row.RowType quite happliy. Might be worth a try for you.
DataControlRowType

Related

How to get GridViewRow from DataKeys

I know this type of question is asked before but no one got the answer yet...!!
How to get a Grid View Row from Data Keys.I don't want to iterate through the whole gird view.
I want to access specific text box(s) in a grid view.
for example in a 100 rows grid view i only want to disable any 2 text boxes on Page Load.
I have Data Key Names defined in grid, but how to get rows from it ?
any idea?
Please try following code..
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Get data row view
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find textbox control
TextBox txtname = (TextBox)e.Row.FindControl("txtName");
string Name = txtname.Text;
if (((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() == "Leave")
{
txtname.disable=true;
}
else
{
txtname.disable = false;
}
}
}

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.

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);
}
}

Selecting an Item in GridView with ITemplate Generated Button

I'm having trouble figuring out how could I use my imagebutton (see below) in my ITemplate to append the button's corresponding row data (ItemID) as a query string.
My ImageButton in my ITemplate:
ImageButton select_button = new ImageButton();
select_button.ID = "select_button";
select_button.ImageUrl = "~/Files/System/Icons/highlighter.png";
select_button.CommandName = "Select";
select_button.ToolTip = "Select";
container.Controls.Add(select_button);
Should I handle it in in the imagebutton's OnClick event (if so, is there a way to get the row where the button is located) or can I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?
I'd be glad to elaborate more on my code upon request. ^ ^
You can set the ID in the CommandArgument property of your button control in the RowDataBound Event. Once you have an ID, you can track rows with it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
((Button)e.Row.FindControl("select_button")).CommandArgument = dr["IdColumn"].ToString();
}
}

Skipping Items During Data Binding in gridview c#

how to skip specific item in row bind event C#
You could
Filter the data via the SQL query.
Filter the data via a table select if binding to a datatable/dataset
Filter data via a DataView
Use the RowDataBound event
Edit ~ Here is how to use the RowDataBound
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow) {
//A value to check for
string myValue = DataBinder.Eval(e.Row.DataItem, "myColumn").ToString();
if ((myValue == "a")) {
//Hide the row
row.Visible = false;
}
}
}
I am not sure how this will affect your paging....if you do have one.

Categories