Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using C#,asp.net,server 2005 and visual studio 2005
my datagrid view design looks like this:
FIRST_COLUMN| SECOND_COLUMN| THIRD_COLUMN| FOURTH_COLUMN| FIFTH_COLUMN|
------------  -------------  ------------  -------------  ------------
TEXTBOX1    | DROPDOWNLIST | TEXTBOX2    | TEXTBOX3     | TEXTBOX4|

i am using the following code to fill the datagrid:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
        //connection.Open();       
        //Int32 modalID = 0;
        SqlDataReader rdr = null;
        //SqlDataReader rdr2 = null;
        try
        {
            string stud = "study1";
            SqlCommand commandToFinding = new SqlCommand("select DateTimeStamp,FindingTypeID,FindingText,SliceID,HowToFindThis from Finding where StudyID='" + stud + "'", connection);
            connection.Open();
            rdr = commandToFinding.ExecuteReader();
            while (rdr.Read())
            {
                e.Row.Cells[0].Text = (rdr.GetDateTime(0)).ToString();
                
                e.Row.Cells[1].Text=(rdr.GetInt32(1)).ToString();
                e.Row.Cells[2].Text = (rdr.GetString(2)).ToString();
                e.Row.Cells[3].Text = (rdr.GetString(3)).ToString();
                e.Row.Cells[4].Text = (rdr.GetString(4)).ToString();                
                rdr.Close();
                break;
            }
            rdr.Close();
        }
}


but when the web page is loaded it doesn't show the datagrid...
what is the problem?
can anybody help me please...
regards
karan
Posted
Updated 8-May-11 21:43pm
v2
Comments
RAJI @Codeproject 9-May-11 3:38am    
Did the query returns any rows?
karan joshua 9-May-11 3:50am    
ya it will return one row...
karan joshua 9-May-11 4:14am    
Is the statement:

e.Row.Cells[0].Text = (rdr.GetDateTime(0)).ToString();

is correct to fill the datagrid column?
thatraja 9-May-11 3:45am    
where is your code in page_load?
karan joshua 9-May-11 3:51am    
if i try to call this in page load it will give an error message saying invalid argument e.

why you are filling data in gridview data bound.i did n't understand.Do one thing create one method
like
public void bindgrid()
{
//your code here.
}
and call it in page load event
if(!ispostback)
{
bindgrid();
}



C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataTable dtToGrid = new DataTable();
               dtToGrid.Columns.Add("UserName", typeof(string));
               dtToGrid.Columns.Add("Password", typeof(string));
               Session["dtToGrid"] = dtToGrid;
           }
       }
       protected void Button1_Click(object sender, EventArgs e)
       {
          //to show textboxes data in gridview
           DataTable dtToGrid = (DataTable)Session["dtToGrid"];
           DataRow drToGrid = dtToGrid.NewRow();
           drToGrid["UserName"] = TextBox1.Text.Trim();
           drToGrid["Password"] = TextBox2.Text.Trim();
           dtToGrid.Rows.Add(drToGrid);
           GridView1.DataSource = dtToGrid;
           GridView1.DataBind();
           TextBox1.Text = "";
       }
       protected void Button2_Click(object sender, EventArgs e)
       {
           //save in Sql Server Table
           cn.Open();
           DataTable dt = (DataTable)Session["dtToGrid"];
           using (SqlBulkCopy copy = new SqlBulkCopy(cn))
           {
               copy.DestinationTableName = "userdetails";
              copy.WriteToServer(dt);
           }
           Page.RegisterStartupScript("<script>", "<script>alert('Successfully Saved')</script>");

       }
 
Share this answer
 
v5
Comments
karan joshua 9-May-11 3:54am    
is it possible to fill the textbox using bindgrid() method.. i have to fill those 5 text boxes with the data from the database
LakshmiNarayana Nalluri 9-May-11 3:57am    
ya see my code you can get an idea.
karan joshua 9-May-11 4:43am    
no
are you able to see a empty datagrid then it should be some problem with the data binding or query result.
try setting a breakpoint and see if you are getting the data inn.
or else problem with the visibility settings of the DG.
 
Share this answer
 
Comments
karan joshua 9-May-11 4:06am    
datagrid is not visible at all during run time...have set visible proprty of DG true..
Mubeen.asim 9-May-11 4:11am    
make sure that you have Not set the visibility from the code behind, and auto generate is set to false.
karan joshua 9-May-11 4:21am    
it is true only..

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