Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datatable in which i am getting rows from database , to print all rows on gridview i have created dynamic datatable and add rows from previous datatable to datarow now, problem is when i print rows on button click it just print first row of datatable.
This is my code.

DataTable Objdatatable = new DataTable();
// Objdatatable = ObjRegBAL.SelectCity(ObjregReports);


DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("name", Type.GetType("System.String"));
dt.Columns.Add("contactdetails", Type.GetType("System.String"));
dt.Columns.Add("Address", Type.GetType("System.String"));
dt.Columns.Add("city_area", Type.GetType("System.String"));
dt.Columns.Add("Visit_time", Type.GetType("System.String"));
dt.Columns.Add("visit_purpose", Type.GetType("System.String"));
dt.Columns.Add("Interaction", Type.GetType("System.String"));
dt.Columns.Add("Already_Computer", Type.GetType("System.String"));
dt.Columns.Add("Software", Type.GetType("System.String"));

string name = "";
string contactdetails = "";
string Address = "";
string city_area = "";
string Visit_time = "";
string visit_purpose = "";
string Interaction = "";
string Already_Computer = "";
string Software = "";
int i = 0;

Objdatatable = UtilityClass.GetData("select (partyname + ' ' + ',' + ' ' + Contact_person) as name, (mobile + ' '+','+' '+Email + ' '+','+' ' + phone) as contactdetails,Address,(City + ' '+','+' '+ Area) as city_area, Visit_time,visit_purpose,Interaction,Already_Computer,Software,area from MarketingCall where city In(" + strSelectedValues_city + ") or area In(" + strSelectedValues_area + ") or software In(" + strSelectedValues_software + ") or calltype In(" + strSelectedValues_calltype + ") or status In(" + strSelectedValues_status + ") or already_computer In(" + strSelectedValues_computer + ") or NextAction_Dt='" + TextBox1.Text + "'", "str_temp");
if (Objdatatable.DataSet.Tables["str_temp"].Rows.Count != 0)
{
for (i = 0; i <= Objdatatable.Rows.Count ; i++)
{
dr = dt.NewRow();
DataRow objdatarow;
objdatarow = Objdatatable.Rows[0];

name = objdatarow["name"].ToString();
dr["name"] = name;

contactdetails = objdatarow["contactdetails"].ToString();
dr["contactdetails"] = contactdetails;

Address = objdatarow["Address"].ToString();
dr["Address"] = Address;

city_area = objdatarow["city_area"].ToString();
dr["city_area"] = city_area;

Visit_time = objdatarow["visit_time"].ToString();
dr["Visit_time"] = Visit_time;

visit_purpose = objdatarow["visit_purpose"].ToString();
dr["visit_purpose"] = visit_purpose;

Interaction = objdatarow["Interaction"].ToString();
dr["Interaction"] = Interaction;

Already_Computer = objdatarow["Already_computer"].ToString();
if (Already_Computer == "False")
{
dr["Already_computer"] = "No";
}
else if (Already_Computer == "True")
{
dr["Already_computer"] = "Yes";
}

Software = objdatarow["software"].ToString();
dr["Software"] = Software;

dt.Rows.Add(dr);
}
}

GridView1.DataSource = dt;
Session["dv"] = dt;
GridView1.DataBind();
GridView1.Visible = true;

ListBox1.ClearSelection();
ListBox2.ClearSelection();
ListBox3.ClearSelection();
ListBox4.ClearSelection();
ListBox5.ClearSelection();
ListBox6.ClearSelection();

if (update1 == "1")
btnselect.Visible = true;
CheckBox1.Visible = true;
Posted

in FOR statement you always use a row with index 0.
C#
objdatarow = Objdatatable.Rows[0];

try this
C#
objdatarow = Objdatatable.Rows[i];
 
Share this answer
 
Comments
[no name] 17-Dec-13 4:18am    
thank you !
just take printdocument control

then swati write below code for datagridview printing and in below code write datatable insted of datagridview



private void button1_Click(object sender, EventArgs e)
{
// printDocument1.Print();
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += printDocument1_PrintPage;
printPreviewDialog.Document = printDoc;
printPreviewDialog.ShowDialog();

}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;





Font ft = new Font("Times New Roman", 10, FontStyle.Regular);
g.DrawString(t, ft, Brushes.Black, new Point(50, 100));

Bitmap bm= new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
Bitmap bm1 = new Bitmap(this.dataGridView2.Width, this.dataGridView2.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
dataGridView2.DrawToBitmap(bm1, new Rectangle(0, 0, this.dataGridView2.Width, this.dataGridView2.Height));
e.Graphics.DrawImage(bm, 100, 200);
e.Graphics.DrawImage(bm1, dataGridView1.Width + 99, 200);




}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900