Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have one form and gridview. i want to display the gridview first row data inside form on page load so that the form gets automatically with the first gridview row data.
I have did these step but nothing happened.
C#
public partial class Flights : System.Web.UI.Page
{
    private string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            FillGrid();
            GridView1.SelectedIndex = 0;
        }     
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        
        FlightDatabase.FlightProperties _flight = new FlightDatabase.FlightProperties();
        if (txtID.Text.Length > 0)
        {
            _flight.ID = Convert.ToInt32(txtID.Text);
        }
        _flight.AirName = txtAirname.Text;
        _flight.Departure = Convert.ToDateTime(txtDeparture.Text);
        _flight.Arrival = Convert.ToDateTime(txtArrival.Text);
        _flight.Duration = txtDuration.Text;
        _flight.Stops = Convert.ToInt32(txtStops.Text);
        _flight.Price = txtPrice.Text;
 
        Session["obj_Airlines"] = _flight;
 
        FlightDatabase.Flight _fl = new  FlightDatabase.Flight();
 
        int rowsAffected=0;
        if (txtID.Text.Length > 0)
        {
            _fl.updateRecords();
        }
        else
        {
            rowsAffected = _fl.insertRecords();
            
        }
 
        if (rowsAffected >= 1)
        {
            lblMsg.Text = "User Registration successful";
        }
        else
        {
            lblMsg.Text = "User Updated successfully";
        }
        GridView1.EditIndex = -1;
        FillGrid();     
    }
 

    private void FillGrid()
    {
        FlightDatabase.Flight _fl = new FlightDatabase.Flight();
        GridView1.DataSource = _fl.FillGrid();
        DataBind();
        _fl = null;
    }
 
    protected void gvw_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
 
        GridViewRow _row=GridView1.Rows[e.NewEditIndex];
        txtID.Text = GridView1.DataKeys[_row.RowIndex].Value.ToString();
        txtAirname.Text = _row.Cells[0].Text;
 
        TextBox textDeparts = (TextBox)_row.Cells[1].FindControl("Label3");
        txtDeparture.Text  = textDeparts.Text;
 
        txtArrival.Text = _row.Cells[2].Text;
        txtDuration.Text = _row.Cells[3].Text;
        
 
        //TextBox txt = (TextBox)this.GridView1.Rows[e.NewEditIndex].FindControl("TextBox1");

        DropDownList labelStops = (DropDownList)_row.Cells[4].FindControl("Label2");
        txtStops.Text = labelStops.Text;
 
        Label labelPrice = (Label)_row.Cells[5].FindControl("Label1");
            
        txtPrice.Text = labelPrice.Text;
 
    }
    
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
    }
    protected void txtPrice_TextChanged(object sender, EventArgs e)
    {
 
    }
 
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow txtStops in GridView1.Rows)
        {
            DropDownList labelStops = (DropDownList)txtStops.Cells[4].FindControl("Label2");
 
            if (int.Parse(labelStops.Text) > 0)
            {
                txtStops.BackColor = System.Drawing.Color.CornflowerBlue;
            }
        }
        foreach (GridViewRow txtPrice in GridView1.Rows)
        {
            Label labelPrice = (Label)txtPrice.Cells[5].FindControl("Label1");
 
            if (decimal.Parse(labelPrice.Text) > 1000)
            {
                txtPrice.BackColor = System.Drawing.Color.Gray;
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        FillGrid();
    }
 
    protected void lbkSelect_Click(object sender, EventArgs e)
    {
        LinkButton btndetails = sender as LinkButton;
 
        GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
 
        txtID.Text = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
        lblAirlineName.Text = gvrow.Cells[0].Text;
 
        TextBox textDeparts = (TextBox)gvrow.Cells[1].FindControl("Label3"); 
 
        lblDeparture.Text = textDeparts.Text;
 
        lblArrival.Text = gvrow.Cells[2].Text;
        lblDuration.Text = gvrow.Cells[3].Text;
 
        DropDownList labelStops = (DropDownList)gvrow.Cells[4].FindControl("Label2");
 
        lblStops.Text = labelStops.Text;
 
        Label labelPrice = (Label)gvrow.Cells[5].FindControl("Label1");
 
        lblPrice.Text = labelPrice.Text;
 
        ctlModalPopupExtender.Show();
    }   
}
Posted
v3
Comments
Jibesh 14-Jan-13 2:09am    
'I have did these step but nothing happened' is really nothing to us. you should clearly mention what went wrong otherwise we cannot do anything. which part of the code doesnt work?
does FillDrid() method really fills anything? have you tried to debug your application?

answer these questions to get your question answered.
HariPrasad katakam 14-Jan-13 3:08am    
You can assign the values to the controls in form in rowdatabound event of gridview. Just put a condition to check row index
for eg :-

IF(e.RowIndex==1)
{
//find the values of gridview row and assign the values to form (controls in side from)
}
Sergey Alexandrovich Kryukov 25-Jan-13 10:45am    
Stop cheating with you fake "answers" like "I solved myself". Why did you accept them formally?
If you want to share a good solution interesting to people, write a Tip & Tricks article...

This is not a big abuse, but you should better know: a number of members lost their memberships due to abuse reports on systematic behavior like that. People posted a number of "problems", followed by "solutions". Even when the "solutions" makes some sense, this defeats the purpose of this forum.

—SA
Arpit Garg1 25-Jan-13 11:19am    
I dont know who you are i asked only the question and if i didn't got the solution for mine so then i tried to solve myself and finally did solve and that's not fake and i think its your attitude problem. i hate this site now because of the people like you i am gonna remove my account from this site where people like you are behaving like this.
Sergey Alexandrovich Kryukov 25-Jan-13 11:55am    
Sorry. I told you what it is, and advised. You decide. Community decides, by voting, and a number of accounts were cancelled. There are reasons why people are irritated by such self-voting. People often tolerate extreme rudeness, to certain extent... You can always add your solutions again, but why?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900