将数据加载到datagrid [英] loading data into datagrid

查看:65
本文介绍了将数据加载到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

推荐答案

为什么要在gridview数据绑定中填充数据.我听不懂.要做一件事创建一个方法
喜欢
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();
}







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

       }


您是否能够看到一个空的数据网格,那么数据绑定或查询结果应该存在一些问题.
尝试设置一个断点,看看是否正在获取数据客栈.
否则DG的可见性设置就会出现问题.
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆