<%@ Page Title="" Language="C#" MasterPageFile="~/firstpage.master" AutoEventWireup="true"
CodeFile="MasterCodes.aspx.cs" Inherits="MasterCodes" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" LoadScriptsBeforeUI="false"
EnablePartialRendering="false">
</asp:ScriptManager>
<div class="contentarea">
<br />
<asp:Label ID="lblsub" Style="overflow-x: auto; margin-left: 20px;" runat="server" Text="Station master data entry" />
</div>
<div style="overflow: auto; padding: 10px 20px;">
<br />
<asp:Label ID="lblcontractnumber" runat="server" Text="Station Code" Font-Names="Calibri"></asp:Label>
<asp:TextBox ID="txtcontractnumber" runat="server"></asp:TextBox>
</div>
<div style="overflow: auto; padding: 10px 20px;">
<asp:Label ID="Label2" runat="server" Text="Station Name: " Font-Names="Calibri"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="3A1-Elevated" Font-Names="Calibri"></asp:Label>
</div>
<div style="overflow: auto; padding: 10px 20px;">
<asp:GridView ID="grd_mastercodes" runat="server" HeaderStyle-BackColor="#f79646" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="false" Font-Names="Calibri"
Font-Size="Medium" RowStyle-BackColor="White" HeaderStyle-BorderColor="Black"
AutoGenerateColumns="False" RowStyle-BorderColor="Black" OnRowCreated="grd_mastercodes_RowCreated">
</asp:GridView>
</div>
</asp:Content>
代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;
public partial class MasterCodes : System.Web.UI.Page
{
bool rowset = false;
GridView dgv = new GridView();
// For First gridview
String[] gridColumnNames_mastercodes = { "S.NO", "WBE Codes", "BOQ ID", "Description", "Unit", "Budgeted Quantity", "Variation Quantity", "Revised Quantity" };
String[] gridColumnwidth_mastercodes = { "50px", "200px", "200px", "350px", "100px", "100px", "100px", "100px" };
int[] gridColumnWidth_mastercodes = { 3, 15, 15, 45, 10, 10, 10, 10 };
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// For First gridview
DataTable dt = new DataTable();
for (int c = 0; c < gridColumnNames_mastercodes.Length; c++)
{
DataColumn dcol = new DataColumn(c.ToString(), typeof(System.Int32));
dcol.ColumnName = gridColumnNames_mastercodes[c];
dt.Columns.Add(dcol);
TemplateField tf = new TemplateField();
tf.HeaderStyle.Font.Bold = false;
tf.HeaderText = gridColumnNames_mastercodes[c];
tf.HeaderStyle.Width = new Unit(gridColumnwidth_mastercodes[c]);
grd_mastercodes.Columns.Add(tf);
}
for (int i = 0; i < 15; i++)
{
dt.Rows.Add(dt.NewRow());
}
grd_mastercodes.DataSource = dt;
grd_mastercodes.DataBind();
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
}
}
//For First Gridview
protected void grd_mastercodes_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int rc = 0; rc < grd_mastercodes.Columns.Count; rc++)
{
TextBox txtBox = new TextBox();
txtBox.ID = "Excel_mastercodes" + e.Row.RowIndex + "_" + rc;
txtBox.MaxLength = gridColumnWidth_mastercodes[rc];
txtBox.Width = new Unit(gridColumnwidth_mastercodes[rc]);
txtBox.BorderStyle = BorderStyle.None;
e.Row.Cells[rc].Controls.Add(txtBox);
e.Row.Cells[rc].Attributes.Add("onKeyDown", "enter(this);");
if (rc == 3)
txtBox.Style["text-align"] = "left";
else
txtBox.Style["text-align"] = "center";
}
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
}
}
}
我需要从sql db中检索数据。我知道我仍然没有连接到数据库,但我很难在上面的代码中编写sql语句来检索数据。如果有人知道意味着请帮助我。 罐
答案 0 :(得分:0)
我有一个用于绑定gridview的示例数据,您可以相应地进行自定义
ASPX代码
<asp:GridView ID="grdhospital"
runat="server"
AutoGenerateColumns="false"
Width="100%"
border="1"
AllowPaging="true"
OnPreRender="PreRenderGrid"
PageSize="2"
OnDataBound="grdhospital_DataBound"
OnPageIndexChanging="grdhospital_PageIndexChanging"
Style="border: 1px solid #E5E5E5;"
CellPadding="3"
CssClass="hoverTable"
DataKeyNames="Id"
OnRowCommand="grdhospital_RowCommand"
OnRowDeleting="grdhospital_RowDeleting"
EmptyDataText="No records found"
ShowFooter="false"
HeaderStyle-CssClass="k-grid td"
EnableSortingAndPagingCallbacks="false">
<AlternatingRowStyle CssClass="k-alt" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="5">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" onClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="hospital_name" HeaderText="Hospital Name" ItemStyle-Width="30" />
<asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="30" />
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" AlternateText="Edit" ImageUrl="~/images/edit.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" />
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS DataBindCode: -
首先,我们需要编写如下所示的BindGrid()
方法
public void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand("Select * from yourtablename");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grdhospital.DataSource = dt;
grdhospital.DataBind();
}
}
}
}
然后我们需要在Page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
希望它对你有所帮助。