我正在动态创建gridview并使用gridview,以下代码正在运行:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (var i = 1; i <= 7; i++)
{
var hDate = new HiddenField { ID = "hDate" + i, Value = string.Format("{0:yyyy-MM-dd}", _weekStart.AddDays(i - 1)) };
e.Row.Cells[i].Controls.Add(hDate);
var hEmployeeId = new HiddenField { ID = "hEmployeeId" + i, Value = ((DataRowView)e.Row.DataItem).Row["EmployeeId"].ToString() };
e.Row.Cells[i].Controls.Add(hEmployeeId);
var lstStart = TimeListBox("lstStart" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "S"].ToString());
e.Row.Cells[i].Controls.Add(lstStart);
var lstEnd = TimeListBox("lstEnd" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "E"].ToString());
e.Row.Cells[i].Controls.Add(lstEnd);
var lstStoreSection = StoreSectionListBox("lstStoreSection" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "SS"].ToString());
e.Row.Cells[i].Controls.Add(lstStoreSection);
}
var lnkView = new LinkButton { ID = "lnkView", Text = "View" };
lnkView.Click += ViewDetails;
lnkView.CommandArgument = ((DataRowView)e.Row.DataItem).Row["Id"].ToString();
e.Row.Cells[8].Controls.Add(lnkView);
}
}
我正在尝试使用RadGrid控制表telerik完成相同的操作。不幸的是,OnRowDataBound方法不存在。有任何想法吗?谢谢
这是完整的代码。基本上它确实创建了一个表示一周7天的网格视图,在每个单元格中我有3个ListBox:一个用于Time开始,一个用于时间结束,另一个用于员工将工作的部分。一切都很好,一些小闪闪发光。但我希望它能与RadGrid一起使用。
public partial class Sample2 : System.Web.UI.Page
{
private DateTime _weekStart, _weekEnd;
private readonly string _strConnString =
ConfigurationManager.ConnectionStrings["EmpireIntranetConnectionString"].ConnectionString;
public SqlConnection Con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetStartAndEndDate();
GenerateGridViewStructure();
}
BindGrid();
}
private void GenerateGridViewStructure()
{
var bfield = new BoundField { HeaderText = "Employé", DataField = "Employee" };
gvSchedule.Columns.Add(bfield);
var tfield = new TemplateField { HeaderText = "Lundi " + string.Format("{0:dd}", _weekStart) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Mardi " + string.Format("{0:dd}", _weekStart.AddDays(1)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Mercredi " + string.Format("{0:dd}", _weekStart.AddDays(2)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Jeudi " + string.Format("{0:dd}", _weekStart.AddDays(3)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Vendredi " + string.Format("{0:dd}", _weekStart.AddDays(4)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Samedi " + string.Format("{0:dd}", _weekStart.AddDays(5)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "Dimanche " + string.Format("{0:dd}", _weekStart.AddDays(6)) };
gvSchedule.Columns.Add(tfield);
tfield = new TemplateField { HeaderText = "View" };
gvSchedule.Columns.Add(tfield);
}
private void BindGrid()
{
var dt = new DataTable();
dt.Columns.AddRange(new[]
{
new DataColumn("Id", typeof (int)),
new DataColumn("EmployeeId", typeof (int)),
new DataColumn("Employee", typeof (string)),
new DataColumn("MondayS", typeof (string)),
new DataColumn("MondayE", typeof (string)),
new DataColumn("MondaySS", typeof (string)),
new DataColumn("TuesdayS", typeof (string)),
new DataColumn("TuesdayE", typeof (string)),
new DataColumn("TuesdaySS", typeof (string)),
new DataColumn("WednesdayS", typeof (string)),
new DataColumn("WednesdayE", typeof (string)),
new DataColumn("WednesdaySS", typeof (string)),
new DataColumn("ThursdayS",typeof(string)),
new DataColumn("ThursdayE",typeof(string)),
new DataColumn("ThursdaySS",typeof(string)),
new DataColumn("FridayS",typeof(string)),
new DataColumn("FridayE",typeof(string)),
new DataColumn("FridaySS",typeof(string)),
new DataColumn("SaturdayS",typeof(string)),
new DataColumn("SaturdayE",typeof(string)),
new DataColumn("SaturdaySS",typeof(string)),
new DataColumn("SundayS",typeof(string)),
new DataColumn("SundayE",typeof(string)),
new DataColumn("SundaySS",typeof(string))
});
Con.ConnectionString = _strConnString;
Con.Open();
var cmd = new SqlCommand
{
Connection = Con,
CommandText = "sp_GetWeeklySchedule '" + string.Format("{0:yyyy-MM-dd}", _weekStart) + "'"
};
using (var dataReader = cmd.ExecuteReader())
{
var i = 0;
while (dataReader.Read())
{
i++;
dt.Rows.Add(i, dataReader["id"], dataReader["Name"].ToString(),
GetSubstringFromReader(dataReader["Monday"].ToString(), 0),
GetSubstringFromReader(dataReader["Monday"].ToString(), 1),
GetSubstringFromReader(dataReader["Monday"].ToString(), 2),
GetSubstringFromReader(dataReader["Tuesday"].ToString(), 0),
GetSubstringFromReader(dataReader["Tuesday"].ToString(), 1),
GetSubstringFromReader(dataReader["Tuesday"].ToString(), 2),
GetSubstringFromReader(dataReader["Wednesday"].ToString(), 0),
GetSubstringFromReader(dataReader["Wednesday"].ToString(), 1),
GetSubstringFromReader(dataReader["Wednesday"].ToString(), 2),
GetSubstringFromReader(dataReader["Thursday"].ToString(), 0),
GetSubstringFromReader(dataReader["Thursday"].ToString(), 1),
GetSubstringFromReader(dataReader["Thursday"].ToString(), 2),
GetSubstringFromReader(dataReader["Friday"].ToString(), 0),
GetSubstringFromReader(dataReader["Friday"].ToString(), 1),
GetSubstringFromReader(dataReader["Friday"].ToString(), 2),
GetSubstringFromReader(dataReader["Saturday"].ToString(), 0),
GetSubstringFromReader(dataReader["Saturday"].ToString(), 1),
GetSubstringFromReader(dataReader["Saturday"].ToString(), 2),
GetSubstringFromReader(dataReader["Sunday"].ToString(), 0),
GetSubstringFromReader(dataReader["Sunday"].ToString(), 1),
GetSubstringFromReader(dataReader["Sunday"].ToString(), 2));
}
}
Con.Close();
gvSchedule.DataSource = dt;
gvSchedule.DataBind();
}
private static string GetSubstringFromReader(string str, int i)
{
if (str == "")
{
return "";
}
return str.Split('-')[i];
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (var i = 1; i <= 7; i++)
{
var hDate = new HiddenField { ID = "hDate" + i, Value = string.Format("{0:yyyy-MM-dd}", _weekStart.AddDays(i - 1)) };
e.Row.Cells[i].Controls.Add(hDate);
var hEmployeeId = new HiddenField { ID = "hEmployeeId" + i, Value = ((DataRowView)e.Row.DataItem).Row["EmployeeId"].ToString() };
e.Row.Cells[i].Controls.Add(hEmployeeId);
var lstStart = TimeListBox("De", "lstStart" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "S"].ToString());
e.Row.Cells[i].Controls.Add(lstStart);
var lstEnd = TimeListBox("À", "lstEnd" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "E"].ToString());
e.Row.Cells[i].Controls.Add(lstEnd);
var lstStoreSection = StoreSectionListBox("lstStoreSection" + i, ((DataRowView)e.Row.DataItem).Row[GetDay(i) + "SS"].ToString());
e.Row.Cells[i].Controls.Add(lstStoreSection);
}
var lnkView = new LinkButton { ID = "lnkView", Text = "View" };
lnkView.Click += ViewDetails;
lnkView.CommandArgument = ((DataRowView)e.Row.DataItem).Row["Id"].ToString();
e.Row.Cells[8].Controls.Add(lnkView);
}
}
private static string GetDay(int i)
{
var day = "";
switch (i)
{
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day = "Sunday";
break;
}
return day;
}
protected void ViewDetails(object sender, EventArgs e)
{
var lnkView = (LinkButton)sender;
var row = (GridViewRow)lnkView.NamingContainer;
var id = lnkView.CommandArgument;
var name = row.Cells[0].Text;
var start = ((ListBox)row.FindControl("lstStart1")).SelectedValue;
var end = ((ListBox)row.FindControl("lstEnd1")).SelectedValue;
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Id: " + id + " Name: " + name + " Start: " + start + " End: " + end + "')", true);
}
private void GetStartAndEndDate()
{
var d = DateTime.Today;
var offset = d.DayOfWeek - DayOfWeek.Monday;
_weekStart = d.AddDays(-offset);
_weekEnd = _weekStart.AddDays(6);
}
protected void btnNext_Click(object sender, EventArgs e)
{
_weekStart = _weekEnd.AddDays(1);
GenerateGridViewStructure();
BindGrid();
}
private ListBox StoreSectionListBox(string name, string value)
{
var lst = new ListBox { ID = name, Rows = 1 };
lst.Items.Add(new ListItem("Section", ""));
Con.ConnectionString = _strConnString;
Con.Open();
var cmd = new SqlCommand
{
Connection = Con,
CommandText = "SELECT * FROM StoreSections"
};
using (var dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
var item = new ListItem
{
Value = dataReader["id"].ToString(),
Text = dataReader["Description"].ToString()
};
lst.Items.Add(item);
}
}
Con.Close();
if (value != null)
lst.SelectedValue = value;
return lst;
}
private static ListBox TimeListBox(string label, string name, string value)
{
var lst = new ListBox { ID = name, Rows = 1 };
lst.Items.Add(new ListItem(label, ""));
var start = new DateTime(1900, 1, 1, 9, 0, 0);
var end = new DateTime(1900, 1, 1, 22, 0, 0);
var current = start;
while (current <= end)
{
lst.Items.Add(new ListItem(current.ToString("HH:mm"), current.ToString("HH:mm")));
current = current.AddMinutes(15);
}
if (value != null)
lst.SelectedValue = value;
return lst;
}
protected void btnSave_Click(object sender, EventArgs e)
{
var totalRows = gvSchedule.Rows.Count;
for (var r = 0; r < totalRows; r++)
{
var row = gvSchedule.Rows[r];
for (int c = 1; c <= 7; c++)
{
var employeeId = (HiddenField)row.FindControl("hEmployeeId" + c);
var vEmployeeId = employeeId.Value;
var date = (HiddenField)row.FindControl("hDate" + c);
var vDate = date.Value;
var start = (ListBox)row.FindControl("lstStart" + c);
var vStart = start.SelectedValue;
var end = (ListBox)row.FindControl("lstEnd" + c);
var vEnd = end.SelectedValue;
if (vStart != "" && vEnd != "")
{
var con = new SqlConnection(_strConnString);
con.Open();
var sql = "sp_UpdateEmployeeSchedule '" + vEmployeeId + "','" + vDate + "','" + vStart + "','" +
vEnd + "'";
var cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
随意给hitn改进我的代码。谢谢
答案 0 :(得分:0)
Telerik的建议是在客户端执行此操作以减少流量,并使用 OnRowDataBound 进一步提高速度,但客户端:
来自Telerik的文档是sample:
<telerik:RadGrid ID="RadGrid1" runat="server">
<ClientSettings>
<ClientEvents OnRowDataBound="RowDataBound" />
</ClientSettings>
</telerik:RadGrid>
<强>的JavaScript 强>
function RowDataBound(sender, args) {
// conditional formatting
if (args.get_dataItem()["TitleOfCourtesy"] == "Dr.") {
args.get_item().get_cell("TitleOfCourtesy").style.fontWeight = "bold";
}
var sb = new Sys.StringBuilder();
sb.appendLine("<b>RowDataBound</b><br />");
for (var item in args.get_dataItem()) {
sb.appendLine(String.format("{0} : {1}<br />", item, args.get_dataItem()[item]));
}
sb.appendLine("<br />");
sb.appendLine("<br />");
$get("<%= Panel1.ClientID %>").innerHTML += sb.toString();
}
答案 1 :(得分:0)
RadGrid有ItemDatabound事件。
而不是 if(e.Row.RowType == DataControlRowType.DataRow) 使用 If(TypeOf e.Item是Telerik.Web.UI.GridDataItem)然后