2007年5月30日

GridView分页(转)

1.控加几个分页要用的
         <tr>
                    <td align="center" style="height: 25px">
                        <asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
                            runat="server">首 页</asp:LinkButton>
                        <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页  </asp:LinkButton>
                        <asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
                        <asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
                        <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                        <asp:Label ID="LblPageCount" runat="server"></asp:Label>
                        <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
                   </td>
                </tr>

2.绑定数据库源
this.NewsView.DataSource = ds.Tables[0];
            this.NewsView.DataBind();

            //分页
            LblCurrentIndex.Text = "第 " + (NewsView.PageIndex + 1).ToString() + " 页";
            LblPageCount.Text = "共 " + NewsView.PageCount.ToString() + " 页";
            LblRecordCount.Text = "总共 "+ds.Tables[0].Rows.Count.ToString()+" 条";
            if (ds.Tables[0].Rows.Count == 0)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
                LblPageCount.Visible = false;
                LblRecordCount.Visible = false;
            }
            else if (NewsView.PageCount == 1)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }

            // 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
            btnFirst.CommandName = "1";
            btnPrev.CommandName = (NewsView.PageIndex == 0 ? "1" : NewsView.PageIndex.ToString());

            btnNext.CommandName = (NewsView.PageCount == 1 ? NewsView.PageCount.ToString() : (NewsView.PageIndex + 2).ToString());
            btnLast.CommandName = NewsView.PageCount.ToString();
            //
        }
        catch(Exception ex)
        {
            Response.Write("数据库错误,错误原因:"+ex.Message);
            Response.End();
        }

3.添加一个事件
protected void PagerButtonClick(object sender, EventArgs e)
    {
        NewsView.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        DataBindintToGridView();
    }

4.PageLoad中
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBindintToGridView();
        }
    }

posted @ 2007-05-30 21:24 pcuseman blog 阅读(136) 评论(1) 编辑

2006年2月13日

一个简单的建立关联的demo

在做libraryMis时遇到的一个问题,我把这个问题缩小简化后,其实很简单,高手们表要笑。
两个表,经过范式化后,每个表中都有ID字段。
r1:

id            name
=====================
001           jack
002           mike
003           lee
004           mary

r2:

id           age
=====================
001          18
002          20
003          21
004          19

通过输入ID,显示出name和age

就这么简单的一个功能,我弄了半天。
问题总结:
1、对string.Format这个方法没有能灵活应用,导致走了弯路。
2、对DataRelation的成员没能足够熟练使用。

主要代码
private void button1_Click(object sender, System.EventArgs e)
  {
   try
   {
    //建立连接
    OleDbConnection conn=new OleDbConnection();
    conn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\study\Relationship\Relationship.mdb;Persist Security Info=False";
    conn.Open();
    DataSet dt=new DataSet();
    string id = textBox1.Text;
    string r1 = string.Format(@"select * from r1 where id = '{0}'",id);//后面的值是输入的ID值,format方法是将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。
    string r2 = string.Format(@"select * from r2 where id = '{0}'",id);

    OleDbDataAdapter odda1=new OleDbDataAdapter(r1,conn);
    OleDbDataAdapter odda2=new OleDbDataAdapter(r2,conn);
    odda1.Fill(dt,"r1");
    odda2.Fill(dt,"r2");
    DataRelation relid=dt.Relations.Add("id",dt.Tables["r1"].Columns["id"],dt.Tables["r2"].Columns["id"]);//建立关联,以各个表中的id列作为关联
    foreach(DataRow prow in dt.Tables["r1"].Rows)
    {
     textBox2.Text=prow["name"].ToString();
     foreach (DataRow crow in prow.GetChildRows(relid))
     {
      textBox3.Text=crow["age"].ToString();
     }
    }
   }
  
   catch(Exception s)
   {
    MessageBox.Show(s.Message);
   }
  }

this demo download

posted @ 2006-02-13 00:08 pcuseman blog 阅读(143) 评论(0) 编辑

2006年2月2日

MDI窗体程序中防止子窗体被多次实例化

有2个Windows Form,主窗体Form1,子窗体ChildForm。在主窗体中加入一Menu,用来实例化子窗体,在Form1.cs中写入下代码:

private static ChildForm childForm; //静态变量,保存唯一实例

private void menuItem2_Click(object sender, System.EventArgs e)
{
ChildForm childForm = GetChildForm(); //获取子窗体对象
childForm.Show(); //显示之
}

private ChildForm GetChildForm()
{
//先后次序不能错,前一个条件用来判断是否是第一次,后一个条件用来判断子窗体是否被关闭了
if( childForm == null || childForm.IsDisposed )
{ //第一次实例化也好,被关了也好,都重新实例化
childForm = new ChildForm();
childForm.MdiParent = this;
}

return childForm;
}

posted @ 2006-02-02 12:18 pcuseman blog 阅读(426) 评论(2) 编辑