how to pass grid data in a gridview template textbox - asp.net

I have a grid view name gvwsponsoor and it has 8 cells every cells has template textbox. if i am double click this textbox then open a model popup. i selected this popup data the data is show the textbox. but second rows are not show data, always data show first rows, how to pass all rows show data, please tell me solution any person .
i try to solved this code but not work in this code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//foreach (GridViewRow grid in gvwSponsor.Rows)
//{
TextBox CostId = (TextBox)gvwSponsor.Rows[0].Cells[2].FindControl("txtCostCenter");
TextBox CostName = (TextBox)gvwSponsor.Rows[0].Cells[1].FindControl("txtDescription");
CostId = (TextBox)gvwSponsor.Rows[0].Cells[2].FindControl("txtCostCenter");
CostName = (TextBox)gvwSponsor.Rows[0].Cells[1].FindControl("txtDescription");
CostId.Text = GridView1.SelectedRow.Cells[2].Text;
CostName.Text = GridView1.SelectedRow.Cells[1].Text;
GridView1.SelectedIndex = -1;
DataTable dtaa = new DataTable();
dtaa = bll.GetNumber(CostId.Text);
if (dtaa.Rows.Count > 0)
{
GridView2.DataSource = dtaa;
GridView2.DataBind();
}
TextBox TxtOp = (TextBox)gvwSponsor.Rows[0].FindControl("txtOP");
TxtOp.Focus();
//}
}

You should process SelectedIndexChanged function of your gridview by using gvwSponsor.SelectedRow, not gvwSponsor.Rows[0]

Related

Row updating is not done

When the Grid view shows the data & when i click on the name it will go for Row Updateing event of the Grid view.All text box are updated but when i make changes in the state & City drop down list,it will not done.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gr = GridView1.Rows[e.RowIndex];
LinkButton lu = new LinkButton();
lu = (LinkButton)gr.FindControl("l2");
HiddenField1.Value = lu.CommandArgument.ToString();
DAL.DAL_EmpReg obj = new DAL.DAL_EmpReg();
DataTable dt = new DataTable();
dt = obj.fetchDataById(HiddenField1.Value.ToString());
if (dt.Rows.Count > 0)
{
txtname.Text=dt.Rows[0]["empName"].ToString();
txtemail.Text = dt.Rows[0]["empEmail"].ToString();
txtcontactno.Text = dt.Rows[0]["empContactNo"].ToString();
txtdeg.Text = dt.Rows[0]["empDesination"].ToString();
txtsalary.Text = dt.Rows[0]["Empsal"].ToString();
ddlstate.SelectedValue = dt.Rows[0]["empState"].ToString();
City(ddlstate.SelectedValue);
ddlcity.SelectedValue = dt.Rows[0]["empCity"].ToString();
txtaddress.Text = dt.Rows[0]["empAddress"].ToString();
}
btnsubmit.Text = "Update";
}
Here state & City two different table.Grid view show the data from the view.City table have the foreign key reference of State table.
Read the below mentioned article. It has doj field that is filled in 3 day,month and year dropdownlists on click of edit button inside gridview and it is updating properly. Hope this will resolve your problem
Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in asp.net C#
http://www.webcodeexpert.com/2013/07/bindsaveeditupdatecanceldeletepaging.html

How to bind data column to textbox?

I have datagridview and textboxes on form. I load records from different tables when user clicks any one radiobutton. Data in grid is shown perfectly. I want to show values of the row in those textboxes.
One solution can be: binding data from dataset.
Second one can be: transfer values of each cell of row to respective textbox.
Please help me. And, please tell me which one is better or is there any other method which is even better than these two.
Thanks in advance.
Try using a BindingSource
BindingSource bindingSource = new BindingSource();
DataSet dataSet = new DataSet();
DataAdapter da1 = new DataAdapter("Select * from Customers", conn1);
DataAdapter da2 = new DataAdapter("Select * form Orders", conn1);
da1.Fill(dataSet,"Customers");
da2.Fill(dataSet,"Orders");
//Let we set Customers table as bindiningSource datasource
bindingSource.DataSource = dataSet.Tables["Customers"];
private void RadioButtonCustomers_CheckedChanged(object sender, EventArgs e)
{
if(radioButtonCustomers.Checked==true)
bindingSource.DataSource =dataSet.Tables["Customers"];
}
private void RadioButtonOrders_CheckedChanged(object sender, EventArgs e)
{
if(radioButtonOrders.Checked==true)
bindingSource.DataSource = dataSet.Tables["Orders"];
}
//First param of Binding is to which prop of TextBox to bind the value
//Second param is the data source
//Third param is the data member or the column name of the table as datasource, so
//we have to get that table from casting the bindingSource datasource prop and casting it
//to DataTable obj and after that to take the ColumnName prop of the desired column
textBox1.DataBindings.Add(new Binding("Text",bindingSource,((DataTable)bindingSource.DataSource).Columns[0].ColumnName));
textBox2.DataBindings.Add(new Binding("Text",bindingSource,((DataTable)bindingSource.DataSource).Columns[1].ColumnName));
etc...
Even if you change the datasource prop of bindingSource, textboxes will remain binded to rowvalue of first and second column
Hope this help.
Sir I done it making a simple WindowsForm with 2 radio buttons, 2 textboxes and datagridview
here is the sln file http://www13.zippyshare.com/v/98590888/file.html this must help u.
I tried many options to display value in textbox on the same form, but it was not working as datagridview could display records of two different tables.
Instead, I used the following event of datagridview:
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
this.dataGridView1.SelectionChanged += new System.EventHandler(this.DisplayValueinTextBox);
}
In DisplayValueinTextBox, I wrote following code based on numbers of columns displayed for each table:
private void DisplayValueinTextBox(object sender, EventArgs e)
{
try
{
textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
if (tblGrid == "Employee") //name of table which has more columns in grid
{
textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[5].Value.ToString();
}
this.dataGridView1.SelectionChanged -= new System.EventHandler(this.DisplayValueinTextBox); //removed it as I was getting error.
}
catch (Exception exdisp)
{
MessageBox.Show(exdisp.Message);
}
}
I also changed SelectionMode property of dataGridView to FullRowSelect. This will ensure that textbox1 is displaying value of SelectedCells[0] even if user clicks any cell.
I still hope there is even a better option, so I wait for comments on this.

GridView as DataTable source sorts only for the first time

I implemented sorting on my GridView with a DataTable as DataSource by using code from this MSDN link. However, my grid sorts for the first time when I click any column, and after that it does not sort on clicking any other column.
Code in the PageLoad() event -
if (!Page.IsPostBack)
{
HView hv = new HView ();
DataTable HTable = new DataTable("hTable");
HTable = hv.FillTable();
Session["hTable"] = HTable;
GridView2.DataSource = Session["hTable"];
GridView2.DataBind();
}
Code in the Sorting event -
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable notesDT = Session["hTable"] as DataTable;
if (notesDT != null)
{
notesDT.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection);
GridView2.DataSource = Session["hTable"];
GridView2.DataBind();
}
}
Does anybody have an idea of what I may be doing wrong?
EDIT: I just realized this. If I select a particular row, I have another view that gets populated with details about that row. When I view some rows details first before trying to sort any columns, then sorting works perfectly fine, any number of times. However, if I try to sort before selecting a row, it works only once.
You are using the DataTable as DataSource in the sorting event, but you should use the sorted view instead. Sorting the view won't change the sort order of the data in the table, just the order in the view.
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable notesDT = Session["hTable"] as DataTable;
if (notesDT != null)
{
notesDT.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection);
GridView2.DataSource = notesDT.DefaultView;
GridView2.DataBind();
}
}
Edit: Although that i've just noticed that you're using rhe same code from MSDN.
You could also try to create a new DataTable from the view:
GridView2.DataSource = notesDT.DefaultView.ToTable(true);
You don't need stored the data table into a session. Actually putting the entire data table into session is not a good idea at all. Any particular reason for that?

How can I get selected row cell value of grid view on page?

I am using a Grid View on my page.
I want to show the data of the selected row cell through response.write(), on the click event of the page button.
Note::
please set the CommandName of your
button to "selectCol"
Please set the CommandName for the
second button , you will use to
delete
to"deleteCol"
Set the command argument property for your button :
.aspx
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
for the two buttons.
.cs
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "selectCol")
{
Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
}
else if(e.CommandName == "deleteCol")
{
int id = int.Parse(gv.DataKeys[index].Value.ToString());//the primary key for your table.
Delete(id);//method which use (Delete From .... Where id = ....).
}
gv.DataBind();
}
catch (Exception ee)
{
string message = ee.Message;
}
}
Greeting Hims.
Easyest way to read value from gridview field is to write:
your_grid_name.SelectedRow.Cell(*number_of_index*).text
In my case that is:
Dim employer_name As String
employer_name=poslodavac_grid.SelectedRow.Cells(1).Text
Just remember that first cell index is zero and that doesn't count "asp:CommandField ShowSelectButton" tag as first one ...
Use GridView.SelectedRow property.
String cellText = this.gridView.SelectedRow.Cells[cellIndex].Text;
Refer to the following to learn about selecting a row in a GridView control.
Select Command in a GridView Control in ASP.Net
If you are using a LINK BUTTON in your grid view, you can use the following code in the ROWCOMMAND method. This code with retrieve all the values in the particular selected row.
// to get the value of the link use the command argument
FaultId = Convert.ToInt32(e.CommandArgument);
// to get the other column values
UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);
Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;
ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;
You can get it in the RowCommand event of the gridview:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Response.Write(row.Cells[0].Text);
Response.Write(row.Cells[1].Text);
................
}
}

How to find control on GridView RowCommand Event?

i tried something like this but did not work:
GridViewRow row = (GridViewRow)(((Repeater)e.CommandSource).NamingContainer);
Repeater _rpt1 = row.Cells[8].FindControl("rptReg") as Repeater;
error:
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.Repeater'.
is there a way i can have the below code in OnRowCommand event using GridView?
actually i am trying to delete the row from the gridview control and when the user click on it and i am grabbing the mulitple ids and passing to SP and update the table and databind the gridview
GridViewRow row = gv.SelectedRow;
Repeater _rpt = gv.Rows[e.RowIndex].Cells[8].FindControl("rptReg") as Repeater;
Repeater _rpt1 = gv.Rows[e.RowIndex].Cells[9].FindControl("rptVisitor") as Repeater;
foreach (RepeaterItem item in _rpt.Items)
{
TextBox _txt = item.FindControl("txtId") as TextBox;
TextBox _txt1 = item.FindControl("txtName") as TextBox;
//update db
}
Please try this:
var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
foreach (GridViewRow gvr in gvDetails.Rows)
{
var btnSelect = (ImageButton)viewRow.FindControl("btnSelect");
if (gvr == viewRow)
{
if (btnSelect.ImageUrl.ToLower() == "~/images/rbnormal.jpg")
{
btnSelect.ImageUrl = "~/Images/rbSelected.jpg";
btnSelect.Enabled = false;
}
}
else
{
btnSelect.ImageUrl = "~/Images/rbnormal.jpg";
btnSelect.Enabled = true;
}
}
Your error is telling me that the control named "rptReg" is a button.
The GridView has a property called DataKeys which can hold multiple IDs
So you would pull out the "PersonId" like:
string personId = GridView1.DataKeys[e.RowIndex]["PersonId"].ToString();

Resources