TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
hadoop hadoop
NA
159
51k
edit link column added every time edit link is clicked
Jul 15 2015 8:08 PM
C# Winform Desktop Application
I've a grid view where I've added a Edit link column from code behind. The edit link works fine when the data is loaded for the first time. But when I do search on the same grid view,
the edit link is added every time I press the Edit link
.
I know the cause of the problem, please see the comment in the code . But I do not know where should I keep my edit function so that it appears in my grid view correctly. Please see my code and suggest.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dashboard.AppCode;
using Dashboard.DAL;
using System.Text.RegularExpressions;
namespace Dashboard.UI
{
public partial class ReceiptReport : Form
{
private bool isSearchMode = false;
public ReceiptReport()
{
InitializeComponent();
isSearchMode = false;
GetReceipt(isSearchMode);
edit();
}
private void ReceiptReport_Load(object sender, EventArgs e)
{
dtpFrom.Value = DateTime.Today.AddDays(-1);
FillPaymentMode();
this.WindowState = FormWindowState.Maximized;
}
private void edit()
{
DataGridViewLinkColumn Editlink = new DataGridViewLinkColumn();
Editlink.UseColumnTextForLinkValue = true;
Editlink.HeaderText = "Edit";
Editlink.DataPropertyName = "lnkColumn";
Editlink.LinkBehavior = LinkBehavior.SystemDefault;
Editlink.Text = "Edit";
receiptGrid.Columns.Add(Editlink);
}
private void receiptGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int paymentTxnId_ = 0;
try
{
if (e.RowIndex >= 0 && receiptGrid.Columns[e.ColumnIndex].Index == 0)
{
DataRowView drv = receiptGrid.Rows[e.RowIndex].DataBoundItem as DataRowView;
paymentTxnId_ = Convert.ToInt32(drv.Row["paymentTxnId"]);
Receipt receiptFrm = new Receipt(paymentTxnId_);
receiptFrm.StartPosition = FormStartPosition.CenterParent;
receiptFrm.Show();
}
}
catch (Exception ex)
{
string title = "Error";
MessageBox.Show(ex.Message.ToString(), title);
}
}
private void GetReceipt(bool mode)
{
int dummyId = 0;
int getPaymentMode = 0;
string fromDate = string.Empty;
string toDate = string.Empty;
string payPartyName = string.Empty;
string recNum = string.Empty;
DataTable dt = new DataTable();
ReceiptDal objDal = new ReceiptDal();
try
{
if (mode == false)
{
dt = objDal.GetReceipt(dummyId);
receiptGrid.DataSource = dt;
receiptGrid.Columns[0].Visible = false;
}
else
{
getPaymentMode = (int)cmbPaymentMode.SelectedValue;
fromDate = dtpFrom.Value.ToShortDateString();
toDate = dtpTo.Value.ToShortDateString();
dt = objDal.SearchReceipt(getPaymentMode, fromDate, toDate);
receiptGrid.DataSource = dt;
receiptGrid.Columns[0].Visible = false;
edit(); // every time it is called when the search button is pressed so added in grid view every time
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
#region search
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
isSearchMode = true;
GetReceipt(isSearchMode);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
#endregion
private void FillPaymentMode()
{
DataTable dt = new DataTable();
ReceiptDal objDal = new ReceiptDal();
dt = objDal.GetPaymentMode();
cmbPaymentMode.DataSource = dt;
cmbPaymentMode.DisplayMember = "paymentmodename";
cmbPaymentMode.ValueMember = "paymentmodeid";
}
}
}
Reply
Answers (
7
)
how to display calender in combobox using windowforms?
How to print datetimepicker to crystal report?