本文介绍了将数据加载到datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#,asp.net,服务器2005和Visual Studio 2005
我的数据网格视图设计如下所示:

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:

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



但是在加载网页时,它不会显示数据网格...
有什么问题吗?
有人可以帮我吗...
问候
karan



but when the web page is loaded it doesn''t show the datagrid...
what is the problem?
can anybody help me please...
regards
karan

推荐答案

public void bindgrid()
{
//your code here.
}
and call it in page load event
if(!ispostback)
{
bindgrid();
}







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

       }



这篇关于将数据加载到datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:38