C# Windows 응용 프로그램에서 DataGridView
의 페이지 매김을 구현하려고합니다. 기본적으로 나는 간단한 DataGridView
데이터베이스의 저장 프로 시저에 의해 채워지고 다른 저장된 프로 시저에서 totalrecords 가져옵니다.DataGridView에서 페이지 매김이 제대로 작동하지 않습니까?
다른 세 가지 폼을 참조하는 그리드에 세 개의 버튼을 추가했으며 매개 변수로 TicketID (그리드의 Column 0)를 보냅니다.
이제 그리드가로드 될 때 완벽하게 작동합니다 (3 개의 버튼 모두 매개 변수로 TicketID를 성공적으로 보냅니다).하지만 페이지 매김 컨트롤 (first, previous, next, last)을 클릭 할 때마다 내가 추가 한 3 개의 버튼이 제대로 작동합니다. 내 말은 TicketID (열 0)를 매개 변수로 보내는 대신 열의 "ButtonName (.Text of DataGridView button)"을 보내는 것입니다.
문제가 무엇인지 파악할 수없는 것 같습니다. 누군가 나를 도울 수 있다면 정말 고마워 할 것입니다. 페이지
코드 :
public partial class Form1 : Form
{
private int totalRecords = 0;
private int mintTotalRecords = 0;
private int mintPageSize = 0;
private int mintPageCount = 0;
private int mintCurrentPage = 1;
public Form1()
{
InitializeComponent();
}
private void fillGrid()
{
try
{
this.mintPageSize = 10;
this.mintTotalRecords = getCount();
this.mintPageCount = this.mintTotalRecords/this.mintPageSize;
if (this.mintTotalRecords % this.mintPageSize > 0)
this.mintPageCount++;
this.mintCurrentPage = 0;
loadPage();
}
catch (Exception ex)
{
}
}
private int getCount()
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = "//connectionstring";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "getTotalNo";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Clear();
con.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
totalRecords = Convert.ToInt32(dr["total"].ToString());
}
}
catch (Exception ex)
{
totalRecords = 0;
}
return totalRecords;
}
private void loadPage()
{
SqlConnection con = new SqlConnection();
try
{
int intSkip = 0;
intSkip = (this.mintCurrentPage * this.mintPageSize);
con.ConnectionString = "//connectionstring";
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "showRecord";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Clear();
com.Parameters.AddWithValue("@pagesize", mintPageSize.ToString());
com.Parameters.AddWithValue("@skip", intSkip.ToString());
con.Open();
SqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dgRecords.DataSource = dt;
label1.Text = (this.mintCurrentPage + 1).ToString() + "/" + this.mintPageCount.ToString();
}
catch (Exception ex)
{
}
}
private void loadbtns()
{
DataGridViewButtonColumn cell = new DataGridViewButtonColumn();
cell.HeaderText = "View Details";
cell.Name = "View";
cell.Visible = true;
cell.Width = 100;
cell.Text = "View Details";
cell.UseColumnTextForButtonValue = true;
DataGridViewButtonColumn cell2 = new DataGridViewButtonColumn();
cell2.HeaderText = "Add Details";
cell2.Name = "Add";
cell2.Visible = true;
cell2.Width = 120;
cell2.Text = "Add Technical Detail";
cell2.UseColumnTextForButtonValue = true;
DataGridViewButtonColumn cell3 = new DataGridViewButtonColumn();
cell3.HeaderText = "Close Ticket";
cell3.Name = "Close";
cell3.Visible = true;
cell3.Width = 100;
cell3.Text = "Close Ticket";
cell3.UseColumnTextForButtonValue = true;
dgRecords.Columns.Add(cell);
dgRecords.Columns.Add(cell2);
dgRecords.Columns.Add(cell3);
}
private void lnkFirst_Click(object sender, EventArgs e)
{
try
{
this.mintCurrentPage = this.mintPageCount - 1;
loadPage();
}
catch
{
}
}
private void lnkNext_Click(object sender, EventArgs e)
{
try
{
this.mintCurrentPage++;
if (this.mintCurrentPage > (this.mintPageCount - 1))
this.mintCurrentPage = this.mintPageCount - 1;
loadPage();
}
catch
{
}
}
private void lnkPrevious_Click(object sender, EventArgs e)
{
try
{
if (this.mintCurrentPage == this.mintPageCount)
this.mintCurrentPage = this.mintPageCount - 1;
this.mintCurrentPage--;
if (this.mintCurrentPage < 1)
this.mintCurrentPage = 0;
loadPage();
}
catch
{
}
}
private void lnkLast_Click(object sender, EventArgs e)
{
try
{
this.mintCurrentPage = 0;
loadPage();
}
catch
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
fillGrid();
loadbtns();
}
void childForm_FormClosed(object sender, FormClosedEventArgs e)
{
this.Visible = true;
}
private void dgRecords_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dgRecords.Columns["Add"].Index)
{
Form5 frm2 = new Form5(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
frm2.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
frm2.Show();
this.Hide();
}
else if (e.ColumnIndex == dgRecords.Columns["Close"].Index)
{
Form6 frm = new Form6(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
frm.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
frm.Show();
this.Hide();
}
else if (e.ColumnIndex == dgRecords.Columns["View"].Index)
{
Form4 frm3 = new Form4(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
frm3.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
frm3.Show();
this.Hide();
}
}
}
}
TotalRecords의 저장 프로 시저 :
ALTER PROCEDURE [dbo].[getTotalNo]
AS
BEGIN
select count(*) total from tblTicketDetail where status = 1
END
ShowRecords의 저장 프로 시저 : 난 그냥 내 자신의 문제를 해결 한
ALTER PROCEDURE [dbo].[showRecord]
@pagesize int,
@skip int
AS
BEGIN
SELECT TOP (@pagesize) * FROM tblTicketDetail WHERE TicketID NOT IN (SELECT TOP (@Skip) TicketID FROM tblTicketDetail)
END
태그를 질문에 적용 할 때주의를 기울여주십시오. ['paging'] (http://stackoverflow.com/tags/paging/info)는 완전히 다른 것을 참조합니다. –
또한 sql 코드를 찾을 수 없습니다. – Alexander