我動態創建一個GridView和使用一個GridView,下面的代碼工作:radgrid控件OnRowDataBound相當於
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天的gridview,在每個單元中我有3個ListBox:一個用於時間開始,一個用於時間結束,另一個用於員工將工作的部分。一切工作正常一些小的閃光點。但我希望它能與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提高我的代碼。謝謝
我修改我最初的問題顯示我的完整代碼。 – Laskar78
乍一看RadGrid看起來並不像你需要的正確選項。有沒有特別的理由使用它? – Dalorzo
我試圖回答我自己的問題http://stackoverflow.com/questions/20887445/getting-started-on-a-employee-schedule-creation – Laskar78