Love?

TRY TO MAKE DREAM COME TRUE

Quảng cáo

---

Chủ Nhật, 23 tháng 1, 2011

Sử dụng sorting / paging trong GridView khi không dùng DataSourceControl

Nếu bạn chọn AllowPaging="true" hoặc AllowSorting="true" trong control GridView mà không dùng DataSourceControl (Ví dụ: SqlDataSource, ObjectDataSource) thì khi bạn chạy sẽ xuất hiện lỗi sau:

Khi chuyển trang trong control GridView control:

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.

Khi click vào cột để sắp xếp

The GridView 'GridViewID' fired event Sorting which wasn't handled.

Lỗi này do không thể thiết lập thuộc tính DataSourceID của GridView vì thế bạn phải thêm sự kiện để sắp xếp và phân trang (sorting and paging)

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging" OnSorting="gridView_Sorting" runat="server" />

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}

Thứ Năm, 20 tháng 1, 2011

Tùy bút 20/01

:D! Mới bước sang ngày mới được gần 1 tiếng :D! Chẳng thấy buồn ngủ gì cả :D! Tự dưng trong người hình như có cảm giác lạ :D! Chẹp chẹp!
Hôm nay sinh nhật lão Thao đây! Trưa nay lại nhậu tiếp ở nhà lão Bằng :D!
Dự án Project-In.NET đang trong giai đoạn phát triển :D! Mặc dù nó hơi lung tung nhưng ít ra nó cũng đang là dự án hoành tránh nhất từ trước tới giờ của mình! Xong 60% Admin Control rồi, :D! Các Fan thì đang mong chờ tết sẽ ra bản chính thức :D! Chắc là không kịp :d Nhưng chắc là lúc trường bắt đầu đi học thì chắc tạm ổn :D!

Thứ Tư, 19 tháng 1, 2011

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed

Hôm nay gỡ VS 2008 ra cài VS 2010 và cài thêm cái Microsoft SQL Server Management Studio Express (SSMSE) nhưng khi vào kết nối cơ sở dữ liệu từ VS 2010 thì gặp lỗi này
"Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed"
:D Hì hục 1 hồi tìm cách giải quyết cuối cùng thì cũng OK :D
Bước 1: Mở SQL Server Configuration Manager.
"Start > Microsoft SQL Server 2008> Configuration Tools > SQL Server Configuration Manager"

Bước 2: Thay đổi thuộc tính Log as on của SQL Server (SQLEXPRESS) thành LocalSystem bằng cách nháy chuột phải chọn properties

Bước 3: Do mình dùng Windows 7 nên xóa thư mục này
"C:\Users\[user]\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS"
Còn nếu bạn dùng Windows XP thì xóa thư mục sau:
"C:\Documents and Settings\[user]\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS"