Extract index(row) from a textbox in a gridview in asp.net - asp.net

Hi i want to extract index (Rows) from a grid view and i use a text box in it. the id should extract from text box in gridview. I use this code:
protected void TextBox_Email_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;
int rowindex = 0;
int idx = currentRow.RowIndex;
string ArticleID = (string)GridView1.DataKeys[idx].Values[1];
}
But the VS gave me this error:
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.GridViewRow'.
Can any body helps me?
Thanks

The parent of the DataControlFieldCell will be the GridViewRow:
protected void TextBox_Email_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent.Parent;
int rowindex = 0;
int idx = currentRow.RowIndex;
string ArticleID = (string)GridView1.DataKeys[idx].Values[1];
}

Related

Add autogenerated Column to gridview

i have AutoGenrated SerialNo of my Gridview in RowDatabound event like below..
protected void Gdview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSerial = (Label)e.Row.FindControl("lblSRNO");
lblSerial.Text = "S" + ((Gdview.PageIndex * Gdview.PageSize) + e.Row.RowIndex + 1).ToString();
}
}
Now i Want to Add this Text to my SQL Column..this SeirlNo is first Column ,,i followed Below procedure to add the values to the Sql Table...
protected void Button1_Click(object sender, EventArgs e)
{
string strOrganization = txtOrganization.Text;
string strOrigin = txtOrigin.Text;
string strLoc = TexLocation.Text;
string strService = TxtService.Text;
string strEst = TxtEstablish.Text;
//SqlCommand cmd = new SqlCommand ("Insert into mpSoftware Organization='"+strOrganization+"'",Origin='"++"',Location='"++"',Service='"++"',Est='"++"' Where SerialNo='"++"');
.....
....
...
But here i strucked..i fogotted that SerialNo is autogenrated..how wud i get the SerialNo value here???? Give me some valuable suggestions plz...
}
Add a public property to hold the value of the Serial No when it is generated. Then pass that value into your sqlCommand

How to select Listbox value in asp.net

I have a ListBox in my webpage that is bound from database in this way:
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
I want to get selected item value and used this code but have a error and does not worked.
ListBox1.SelectedValue;
Forgive me if I have problems on how to write because my English is not good.
Can you be more specific on the error you are getting?
Using ListBox1.SelectedValue should work.
For example:
int mySelectedValue = int.Parse(ListBox1.SelectedValue);
or
string mySelectedValue = ListBox1.SelectedValue;
Edit
Added code to ensure the original poster was keeping values in ListBox databound.
protected void Page_Load( object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListBox();
}
}
private BindListBox()
{
ListBox1.DataTextField = "Text";
ListBox1.DataValueField = "MenuID";
ListBox1.DataSource = SqlHelper.ExecuteReader(DAL.DALBase.ConnectionString, "GetMenu");
ListBox1.DataBind();
}
protected void SomeButton_Click( object sender, EventArgs e)
{
string mySelectedValue = ListBox1.SelectedValue;
}

how to update a data table row value to hyperlink?

I have a data table in dt in c# code and it has column[0] datatype is int.So when ever I reach 7th value in the table I need to convert to hyperlink and add it back to data table.
int x = int.Parse(dt.Rows[7][0].ToString());
dt.Row[7][0] = "" + x + "";
but it is givin me the error that can not accept string value to integer. How to over come this?
Correct me I'm wrong. Add an extra column of string type.
dt.Columns.Add("LinkColumn");
...
dt.Rows[7]["LinkColumn"]=string.Format("<a href='#'>{0}</a>",x);
How are you creating the data table? The column has probably been typed to int, so it can not accept string values.
A better way might be to change the data bound control to show the link
protected override void OnInit(EventArgs e) {
base.OnInit(e);
DataBound += new EventHandler(GridView1_DataBound);
}
void GridView_RowDataBound(object sender, GridViewRowEventArgs e) {
GridView gridview = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow) {
for (int i = 0; i < gridview.Columns.Count; i++) {
DataControlField obj1 = gridview.Columns[i];
if (obj1.GetType() == typeof(BoundField)) {
BoundField field = (BoundField)obj1;
string datafield = field.DataField;
object value = DataBinder.Eval(e.Row.DataItem, datafield);
Literal c = new Literal();
c.Text = "";
e.Row.Cells[i].Controls.Add(c);
}
}
}
}

asp.net making a gridView column invisible

This is a Master-Detail form. Master is a GridView. And, the Detail is a DetailsView.
The entire thing is achieved programmatically.
As you can see from the code, DetailsView is using the Master-objects's ID to retrieve the Detail items.
I need to make the ID column of the Master-GridView invisible. Coz, it is irrelevent for the user of the page. But it must not harm the page logic.
But the code-line, GridView1.Columns[1].Visible = false; is generating an exception.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How should I solve this problem?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
List<Order> orders = Order.Get();
GridView1.DataSource = orders;
GridView1.DataBind();
// This is giving Error...............!!!
GridView1.Columns[1].Visible = false;
// At first, when the page first loads,
// GridView1.SelectedIndex == -1
// So, this is done to automatically select the 1st item.
if (GridView1.SelectedIndex < 0)
{
GridView1.SelectedIndex = 0;
}
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView1.Rows[selRowIndex].Cells[1].Text);
Order master = Order.Get(selMasterId);
labItemsCount.Text = master.Items.Count.ToString();
DetailsView1.DataSource = master.Items;
DetailsView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindData();
}
}
Have you considered using the DataKeyNames property of the gridview? This way you can remove the 'id' column from the GridView bu still access the 'id' value in the Page_Load.
DataKeyNames = "id"
Then you can get the value of the id like this.
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView.DataKeys[selRowIndex].Value);
Order master = Order.Get(selMasterId);
Alternately, you could try changing the visibility of the column in the OnRowBound event of the GridView.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}

How do I find a GridView item at button_click?

I am using the following code:
foreach (GridViewRow dr in gvrejectreq.Rows)
{
DropDownList ddl=(DropDownList)gvrejectreq.Rows[dr.RowIndex].FindControl("DropDownList1");
string status = ddl.SelectedValue;
int userid = gvrejectreq.Rows[dr.RowIndex].
}
If I understand what you're asking...you don't need to loop...
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e){
if (e.CommandName == "thiscommandname") {
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//and then for example...
string rowText = (LinkButton)selectedRow.Cells[0].Controls[0]).Text;}
}

Resources