⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbclass.cs

📁 贸易有限公司项目管理系统(C#.NET2003),供学习参考!
💻 CS
📖 第 1 页 / 共 2 页
字号:
    /// <returns>选中的项DataKeys值</returns>
    public static string SelectedCode(DataGrid DG)
    {
        string thisCode = null;
        if (DG.Items.Count == 0)
        {
            WriteMessage("目前没有数据,请添加!", true, true);
            return null;
        }
        if (SelectedIndex(DG) == -1)
        {
            WriteMessage("请选择一项!", true, true);
            return null;
        }
        else
        {
            if (DG.DataKeyField.ToString().Trim().Length != 0)
            {
                thisCode = DG.DataKeys[SelectedIndex(DG)].ToString().Trim();
                return thisCode;
            }
            else
            {
                WriteMessage("DataGrid没有设置DataKeyField,因此无法确定选择项!", true, true);
                return null;
            }
        }
    }
    public static void BindDataGrid(string strsql, DataGrid dg)
    {
        try
        {
            Open();
            Fill(strsql);
            dg.DataSource = ds.Tables[0].DefaultView;
            if (dg.CurrentPageIndex > dg.PageCount - 1)
            {
                if (dg.PageCount > 0)
                {
                    dg.CurrentPageIndex = dg.PageCount - 1;
                }
                else
                {
                    dg.CurrentPageIndex = 0;
                }
            }
            dg.DataBind();
        }
        catch (Exception e)
        {
            WriteMessage(e.Message.ToString().Trim(), true, true);
        }
        finally
        {
            Close();
            Dispose();
        }
    }
    public static void BindDataGrid1(string sql, DataGrid mydg)
    {
        Fill(sql);
        mydg.DataSource = ds.Tables[0].DefaultView;
        mydg.DataBind();
    }
    //若datagrid有多页,而被删除项处于第n(n>1)页且该页只有1条数据时,删除后会导致页数减去1,再次绑定datagrid时
    //仍然定位到该页引发错误.
    //故定义此函数防止此中错误出现
    //执行删除语句后,再次绑定前,调用此函数即可
    public static void DeleteDgNotice(DataGrid dg)
    {
        if ((dg.Items.Count % dg.PageSize == 1) && (dg.PageCount > 1))
        {
            if (dg.PageCount > 1)
            {
                dg.CurrentPageIndex = dg.CurrentPageIndex - 1;
            }
            else
            {
                dg.CurrentPageIndex = 0;
            }
        }
    }
    //利用sql语句绑定给定的datagrid
    //并将其中一列进行UrlEndcode 以作为超链接的参数
    //使用该函数时 ,将datagrid超链接的url字段处填写"keyfield"即可
    //strql表示sql语句
    //dg表示给定的datagrid
    //columnname表示需要UrlEncode的列名
    public static void BindDataGrid(string strsql, DataGrid dg, string ColumnName)
    {
        try
        {
            if (myConnection.State == ConnectionState.Closed)
            {
                Open();
            }
            Fill(strsql);
            ds.Tables[0].Columns.Add("KeyField");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ds.Tables[0].Rows[i]["KeyField"] = HttpUtility.UrlDecode(ds.Tables[0].Rows[i][ColumnName].ToString().Trim());
                dg.DataSource = ds.Tables[0].DefaultView;
                if (dg.CurrentPageIndex > dg.PageCount - 1)
                {
                    if (dg.PageCount > 0)
                    {
                        dg.CurrentPageIndex = dg.PageCount - 1;
                    }
                    else
                    {
                        dg.CurrentPageIndex = 0;
                    }
                }
            }
            dg.DataBind();
        }
        catch (Exception e)
        {
            WriteMessage(e.Message.ToString().Trim(), true, true);
        }
        finally
        {
            Close();
            Dispose();
        }
    }
    public static void BindDropDownList(string str_Text, string sql, DropDownList myDropDownList)
    {
        // 绑定DropDownList控件(注:四个函数,该函数需要一个字段名,分别绑定Value和Text两值,默认表名)
        //Open();
        if (ds != null)
        {
            FillAdd("binddropdownlist" + sql.ToString(), sql);
            myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
        }
        else
        {
            Fill(sql);
            myDropDownList.DataSource = ds.Tables[0].DefaultView;
        }

        myDropDownList.DataValueField = str_Text;
        myDropDownList.DataTextField = str_Text;
        myDropDownList.DataBind();
    }

    /// <summary>
    /// 绑定DropDownList控件并显示数据,DropDownList控件Value,Text值将分别等于等于str_Value,str_Text值
    /// </summary>
    /// <param name="str_Value">绑定DropDownList控件Value值相对应数据库表字段名</param>
    /// <param name="str_Text">绑定DropDownList控件Text值相对应数据库表字段名</param>
    /// <param name="sql">Select-SQL语句</param>
    /// <param name="myDropDownList">DropDownList控件id值</param>
    public static void BindDropDownList(string str_Value, string str_Text, string sql, DropDownList myDropDownList)
    {

        try
        {
            if (myConnection.State == ConnectionState.Closed)
            {
                Open();
            }
            if (ds != null)
            {
                FillAdd("binddropdownlist" + sql.ToString(), sql);
                myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
            }
            else
            {
                Fill(sql);
                myDropDownList.DataSource = ds.Tables[0].DefaultView;
            }
            myDropDownList.DataValueField = str_Value;
            myDropDownList.DataTextField = str_Text;
            myDropDownList.DataBind();
            if (myDropDownList.Items.Count == 0)
            {
                ListItem li_null = new ListItem("无", "无");
                myDropDownList.Items.Add(li_null);
            }
        }
        catch (Exception e)
        {
            WriteMessage(e.Message.ToString().Trim(), true, true);
        }
        finally
        {
            Close();
            Dispose();
        }

    }

    public static void BindDropDownList(string str_Value, string str_Text, string sql, DropDownList myDropDownList, bool all)
    {

        if (ds != null)
        {
            FillAdd("binddropdownlist" + sql.ToString(), sql);
            if (all)
            {
                DataRow drL = ds.Tables["binddropdownlist" + sql.ToString()].NewRow();
                drL[str_Text] = "";
                drL[str_Value] = "";
                ds.Tables["binddropdownlist" + sql.ToString()].Rows.InsertAt(drL, 0);
            }
            myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
        }
        else
        {
            Fill(sql);
            if (all)
            {
                DataRow drL = ds.Tables[0].NewRow();
                drL[str_Text] = "";
                drL[str_Value] = "";
                ds.Tables[0].Rows.InsertAt(drL, 0);
            }
            myDropDownList.DataSource = ds.Tables[0].DefaultView;
        }
        myDropDownList.DataValueField = str_Value;
        myDropDownList.DataTextField = str_Text;
        myDropDownList.DataBind();
        //Close();
    }

    /// <summary>
    /// 绑定DropDownList控件,取得选中值
    /// </summary>
    /// <param name="str_Value">数据库表示Value值字段</param>
    /// <param name="str_Text">数据库表示Text值字段</param>
    /// <param name="str_Value_Field">选中项目的值</param>
    /// <param name="str_Sql">绑定数据的SQL语句</param>
    /// <param name="myDropDownList">下拉列表框的名称</param>
    public static void SelectBindDropDownListValue(string str_Value, string str_Text, string str_Value_Field, string str_Sql, DropDownList myDropDownList)
    {
        BindDropDownList(str_Value, str_Text, str_Sql, myDropDownList);// 绑定myDropDownList控件
        myDropDownList.Items[0].Selected = false;
        for (int i = 0; i < myDropDownList.Items.Count; i++)
        {
            if (str_Value_Field == myDropDownList.Items[i].Value)
            {
                myDropDownList.Items[i].Selected = true;
                break;
            }
        }
    }
    //以javascript的windous.alert()方法输出提示信息
    //strmsg 表示要输出的信息
    //back 表示输出后是否要history.back()
    //end 表示输出后是否要Response.End()
    public static void WriteMessage(string strMsg, bool Back, bool End)
    {
        Response = HttpContext.Current.Response;
        //将单引号替换,防止js出错
        strMsg = strMsg.Replace("'", "");
        //将回车符号换掉,防止js出错
        strMsg = strMsg.Replace("\r\n", "");
        Response.Write("<script language=javascript>alert('" + strMsg + "')</script>");
        if (Back)
        {
            Response.Write("<script language=javascript>history.back();</script)");
        }
        if (End)
        {
            Response.End();
        }
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -