如何只填充一次datagrid?它被反复填充...... [英] How to fill datagrid only once? it is filled repeatedly...
本文介绍了如何只填充一次datagrid?它被反复填充......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Petrol_Billing_System1
{
public partial class frmDisplayCreditSales : Form
{
public frmDisplayCreditSales()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(Properties.Settings.Default.dbconnection);
DataSet ds = new DataSet();
private void btnNew_Click(object sender, EventArgs e)
{
frmCreditSalesDetails frm = new frmCreditSalesDetails();
frm.Show();
}
private void frmDisplayCreditSales_Load(object sender, EventArgs e)
{
dtpdisCreditSale.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
dtpdisCreditSale.CustomFormat = "dd MMMM,yyyy";
dtpdisCreditSale.Format = DateTimePickerFormat.Custom;
dtpFromDate.CustomFormat = "dd MMMM,yyyy";
dtpFromDate.Format = DateTimePickerFormat.Custom;
dtpToDate.CustomFormat = "dd MMMM,yyyy";
dtpToDate.Format = DateTimePickerFormat.Custom;
SqlDataAdapter daitems = new SqlDataAdapter("Select ItemID,ItemName from Items", con);
daitems.Fill(ds, "items");
SqlDataAdapter dacust = new SqlDataAdapter("Select CustomerID,CustomerName from Customers", con);
dacust.Fill(ds,"cust");
SqlDataAdapter dadt = new SqlDataAdapter("select s.SalesID,c.CustomerID,chd.SalesID,chd.ItemID,ch.ChallanID,ch.Date,s.FromDate,s.ToDate,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from Challans ch inner join ChallanDetails chd on chd.ChallanID=ch.ChallanID inner join Sales s on s.CustomerID=ch.CustomerID inner join Customers c on c.CustomerID=s.CustomerID", con);
dadt.Fill(ds, "dt");
bndSoDisCrSale.DataSource = ds;
bndSoDisCrSale.DataMember = "dt";
cmbCustomerName.DisplayMember = "CustomerName";
cmbCustomerName.ValueMember = "CustomerID";
cmbCustomerName.DataSource = ds.Tables["cust"];
txtInvoiceNo.DataBindings.Add("Text", bndSoDisCrSale, "SalesID");
dtpdisCreditSale.DataBindings.Add("Value", bndSoDisCrSale, "Date");
cmbCustomerName.DataBindings.Add("SelectedValue", bndSoDisCrSale,"CustomerID");
dtpFromDate.DataBindings.Add("Value", bndSoDisCrSale, "FromDate");
dtpToDate.DataBindings.Add("Value", bndSoDisCrSale, "ToDate");
string sql = string.Format("select ch.Date,ch.ChallanID,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from ChallanDetails chd inner join challans ch on ch.ChallanID=chd.ChallanID where chd.SalesID='{0}'", txtInvoiceNo.Text);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds, "gd");
grddiscrsale.DataSource = ds;
grddiscrsale.DataMember = "gd";
ds.Tables["gd"].RowChanged += new DataRowChangeEventHandler(frmDisplayCreditSales_RowChanged);
int total = 0;
for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
{
total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());
}
txtTotalPrice.Text = total.ToString();
}
void frmDisplayCreditSales_RowChanged(object sender, DataRowChangeEventArgs e)
{
decimal Total = 0;
foreach (DataRow row in ds.Tables["gd"].Rows)
{
Total += decimal.Parse(row["Total"].ToString());
}
txtTotalPrice.Text = Total.ToString();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnEdit_Click(object sender, EventArgs e)
{
string sql = string.Format("Select SalesID from ChallanDetails where SalesID='{0}'",txtInvoiceNo.Text);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
con.Open();
sdr = cmd.ExecuteReader();
sdr.Read();
string salesID = sdr["SalesID"].ToString();
sdr.Close();
con.Close();
frmEditCreditSalesDetails frm= new frmEditCreditSalesDetails(txtInvoiceNo.Text,this);
frm.Show();
}
private void bndnavdiscrsale_RefreshItems(object sender, EventArgs e)
{
int total = 0;
for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
{
total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());
}
txtTotalPrice.Text = total.ToString();
}
推荐答案
你可以试试的东西。
将来请只显示问题发生的位置代码。
Something you can try.
In future please only show the code of where the problem is occurring.
bool filled = false;
private void btnNew_Click(object sender, EventArgs e)
{
frmCreditSalesDetails frm = new frmCreditSalesDetails();
frm.Show();
}
private void frmDisplayCreditSales_Load(object sender, EventArgs e)
{
if(filled == false)
{
DataGridFill();
}
}
public static void DataGridFill()
{
dtpdisCreditSale.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
dtpdisCreditSale.CustomFormat = "dd MMMM,yyyy";
dtpdisCreditSale.Format = DateTimePickerFormat.Custom;
dtpFromDate.CustomFormat = "dd MMMM,yyyy";
dtpFromDate.Format = DateTimePickerFormat.Custom;
dtpToDate.CustomFormat = "dd MMMM,yyyy";
dtpToDate.Format = DateTimePickerFormat.Custom;
SqlDataAdapter daitems = new SqlDataAdapter("Select ItemID,ItemName from Items", con);
daitems.Fill(ds, "items");
SqlDataAdapter dacust = new SqlDataAdapter("Select CustomerID,CustomerName from Customers", con);
dacust.Fill(ds,"cust");
SqlDataAdapter dadt = new SqlDataAdapter("select s.SalesID,c.CustomerID,chd.SalesID,chd.ItemID,ch.ChallanID,ch.Date,s.FromDate,s.ToDate,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from Challans ch inner join ChallanDetails chd on chd.ChallanID=ch.ChallanID inner join Sales s on s.CustomerID=ch.CustomerID inner join Customers c on c.CustomerID=s.CustomerID", con);
dadt.Fill(ds, "dt");
bndSoDisCrSale.DataSource = ds;
bndSoDisCrSale.DataMember = "dt";
cmbCustomerName.DisplayMember = "CustomerName";
cmbCustomerName.ValueMember = "CustomerID";
cmbCustomerName.DataSource = ds.Tables["cust"];
txtInvoiceNo.DataBindings.Add("Text", bndSoDisCrSale, "SalesID");
dtpdisCreditSale.DataBindings.Add("Value", bndSoDisCrSale, "Date");
cmbCustomerName.DataBindings.Add("SelectedValue", bndSoDisCrSale,"CustomerID");
dtpFromDate.DataBindings.Add("Value", bndSoDisCrSale, "FromDate");
dtpToDate.DataBindings.Add("Value", bndSoDisCrSale, "ToDate");
string sql = string.Format("select ch.Date,ch.ChallanID,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from ChallanDetails chd inner join challans ch on ch.ChallanID=chd.ChallanID where chd.SalesID='{0}'", txtInvoiceNo.Text);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds, "gd");
grddiscrsale.DataSource = ds;
grddiscrsale.DataMember = "gd";
ds.Tables["gd"].RowChanged += new DataRowChangeEventHandler(frmDisplayCreditSales_RowChanged);
int total = 0;
for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
{
total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());
}
txtTotalPrice.Text = total.ToString();
filled = true;
}
你可以在页面加载中绑定数据
private void frmDisplayCreditSales_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
DataGridFill();
}
}
Public Void DataGridFill()
{
将数据绑定到DataGrid这里....
}
Hi,
U Can Bind the Data in the Page Load
private void frmDisplayCreditSales_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataGridFill();
}
}
Public Void DataGridFill()
{
Bind Data to DataGrid Here....
}
这篇关于如何只填充一次datagrid?它被反复填充......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文