C# .NET - DataGridView
Asked By Esmail
02-Sep-10 07:37 AM
I have added a check box colum.
I want that If user selects a checkbox that row should be deleted by using a button control named Delete
Give me the code if possible
Thank You
Reena Jain replied to Esmail
hi,
just use the following code on delete button code
public string FindDt()
{
int j = 0, k = 0;
string ChannelCd = "";
DataTable dt = new DataTable();
for (int i = 0; i < dgChannel.RowCount; i++)
{
if (dgChannel.Rows[i].Cells[0].Value == null)
{
j = j + 1;
}
else if ((bool)dgChannel.Rows[i].Cells[0].Value == true)
{
k = k + 1;
ChannelCd = dgChannel.Rows[i].Cells[3].Value.ToString();
}
else if ((bool)dgChannel.Rows[i].Cells[0].Value == false)
{
j = j + 1;
}
}
if (j == dgChannel.RowCount)
{
ClsCommon.Error = 8;
FrmError obj = new FrmError();
obj.ShowDialog();
ChannelCd = "";
}
else if (k > 1)
{
ClsCommon.Error = 9;
FrmError obj = new FrmError();
obj.ShowDialog();
ChannelCd = "";
}
return ChannelCd;
}
here cell is the unique value of row.
in channelcd this unique value would be get and then provide this unique value in database to delete the row and again bind the datagridview with new data
hope this will help you
Sagar P replied to Esmail
If you have inserted check box as first cell then use this code, other wise give your checkbox cell index properlly;
for (i = 0; i <= dataGridView.Rows.Count - 1; i++)
{
if (dataGridView.Rows[i].Cells[0].Value != null)
{
if ((bool) dataGridView.Rows[i].Cells[0].Value == true)
{
dataGridView.Rows.RemoveAt(i);
}
}
}
Sreenivasulu Pitla replied to Esmail
Try the following code for delete button click event.
private void button1_Click(object sender, EventArgs e)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (DataGridViewRow row in customersDatagrid.Rows)
{
// find the checked row and ad it to the list
if (row.Index != -1 && row.Cells[2].Value != null && Convert.ToBoolean(row.Cells[2].Value.ToString()))
{
list.Add(row.Cells[0].Value);
}
}
this.deleteRows(list); // delete the list of rows from database
this.fillDataGrid(); // re-fill the datagrid
}
Let me know if you need code for deleteRows() and fillDataGrid()
Sreenivasulu P
keep track of your daily expenses @ www.khurch.com

set to instance of an object Hello all, kindly help me in fixing the Subject error. this c# code works for timeshhet which is conected to our electronic mangement system on mabdalla \ desktop \ time sheet project \ time_card.cs:line 1382 at time_sheet.Time_Card.Time_Card_Activated(Object sender, EventArgs e) in c: \ documents and settings \ mabdalla \ desktop \ time sheet project \ time_card.cs:line 1251 at System.Windows.Forms.Form.OnActivated(EventArgs e) at System.Windows.Forms.Form.set_Active(Boolean value) at System.Windows.Forms.Form.WmActivate TextBox txtNumberOfHours1; private System.Windows.Forms.TextBox txtNumberOfHours2; private System.Windows.Forms.Button btnLoadPeriodData; private bool alert_open = false; #endregion System.DateTime currentDate; private System.Windows.Forms.Button btnPrevDay; private System.Windows.Forms.Button btnNextDay; private System.Windows.Forms.Button btnShowDG; public static bool ExcelChanged = false; public Time_Card() { } public Time_Card( VBC_USER vbc_user) { / / / / Required for Windows Form Designer support / / InitializeComponent VBC_Classes.vbc_user = vbc_user; } / / / <summary> / / / Clean up any resources being used. / / / < / summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components ! = null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer false); } #endregion [STAThread] static void Main() { Application.Run(new Login()); } private void firsttime_onload() { System.Collections.ArrayList myWorkTypeList = VBC_Classes.vbcWS.get_Properties(null).get_Item(VBC_Classes.vbc_user.REQUIRED_PROPERTIES[0]).Values.Items; foreach(VBC.OM checkBox2.Enabled = false; this.listViewEx1.Enabled = false; this.ClearTextBoxes(); } private void Form1_Load(object sender, System.EventArgs e) { btnAcknoledge.Visible = true; firsttime_onload(); onload(); } private void button1_Click(object sender, System.EventArgs e) { / / this
Exporting Employee info to excel hi, i am facing one error.please help urgent for me. i am exporting data in web page to excel but please help. u can do it like below take ur gridview data in to the datatable and then follow below steps public DataTable GetDataTableFromExcel( DataTable dt) { try { dt.Columns.Add( "RStatus" ); dt.Columns.Add( "Remarks" ); dt.AcceptChanges(); int j; foreach Data.SqlClient; public partial class ExportGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = BindData(); GridView1.DataBind(); } } private string ConnectionString { get { return @"Server DataSet ds = new DataSet(); ad.Fill(ds, "Categories"); return ds; } protected void FiilData_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename = FileName.xls"); Response.Charset = ""; / / If you public class ExcelExporterTool { public ExcelExporterTool() { / / / / TODO: Add constructor logic here / / } public string ExportGridViewDataToExcel ( GridView source, bool randomFilename, string nameOfFile) { if (source ! = null) { string fileName = string.Empty; if (randomFilename) { fileName = Guid.NewGuid so now let's move to codebehind Button1_Click event handler: protected void Button1_Click(object sender, EventArgs e) { ExcelExporterTool exporter = new ExcelExporterTool(); string fileLocation = exporter. ExportGridViewDataToExcel( GridView1, true, ""); if (fileLocation ! = null) { Response
trying to add different type of variable (e.g. int), it will throw compile time error. • In another sense, if we have string and int in a same list then it will throw a compile time error. • Generics are available System.Collections.Generic Namespace. • Generics introduce to the .NET Framework the concept Grid View DataSource to lst variable, like GridView1.DataSource = lst; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { / / Creating Variable List List<String> lst = new List<string> (); / / Adding Data lst DataSource = lst; GridView1.DataBind(); } } 2. Can we able to use Generic list in Dataset or Datatable or Datareader using to bind in Grid view? If you are interacting with database then experience. For example, I'm sure most of us are familiar with the System.Collection.ArrayList class in Framework v1 and v1.1. If you have used it at all, you thrown even if you add objects of different types to a single list) System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add("a string"); list.Add(45); / / no exception thrown list.Add(new System.Collections.ArrayList()); / / no exception foreach(string s in list) { / / exception will be thrown! System.Console.WriteLine(s
page.Please isolate this code into DataAccessLayer, BusinessLayer and PresentationLayer. protected void Page_Load(object sender, EventArgs e) { / / This function fills the drop down controls for day, month and year. int startDate lblMessage.Text = "* No accounts available to show statement."; return; } } catch (SqlException ex) { lblMessage.Text = "Sorry, error while Login" +ex.Message; return; } finally { dr.Clear(); conn.Close(); } } Thanks, Sumit Presentation Layer (UI com = new Sqlcommand(sql, Conn); SqlDataReaderdr; dr = com.ExecuteReader(); catch (SqlException ex) { lblMessage.Text = "Sorry, error while Login" +ex.Message; return; } finally { dr.Clear(); conn.Close(); } } should be in DAL since mean aspx.cs page. Here is your code which i written: public class AccountDAL { public DataTable LoadAccountData(string userName) { / / This function loads the Account Ids into a DropDownList control for the customer who is logged in the system. try { DataTable data = new DataTable(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { String sql = "SELECT [AccountID] FROM [Customer_Account Load(dr); } } } return data; } catch (SqlException ex) { throw ex; } } public class AccountBAL { AccountDAL accountDAL; public DataTable GetAccountData(string username) { if (accountDAL = = null) { accountDAL = new AccountDAL(); } return accountDAL.LoadAccountData(username); } } After this Hi i look your code , it to overhead code man private void button2_Click( object sender, EventArgs e) { DirectoryInfo dirMain = new DirectoryInfo( "D: \ test" ); if (dirMain ! = null ) { FileInfo[] dirRar = dirMain.GetFiles( "*.zip
password . . . . Thanks &Regards Untitled document Try this code for loginCheck: protected void BtnLogin_Click( object sender, EventArgs e) { String conn = "<Your connectionSTring> " ; SqlConnection connection = new SqlConnection (conn); connection.Open(); string q = "select Close(); } Now at home page write this in page load protected void Page_Load( object sender, EventArgs e) { if (Session[ "user" ] ! = null ) { Label1.Text = "Welcome " + Session[ "user" ].ToString(); } } HI PLEASE TRY LIKE finally { cn.Close(); } <script type = "text / javascript"> function checkForm(form) { if(form.username.value = = "") { alert("Error: Username cannot be blank!"); form.username.focus(); return false; } re = / ^ \ w+$ / ; if(!re.test(form.username.value)) { alert("Error: Username must contain only letters, numbers and underscores!"); form.username.focus(); return false; } if(form pwd1.value ! = "" && form.pwd1.value = = form.pwd2.value) { if(form.pwd1.value.length < 6) { alert("Error: Password must contain at least six characters!"); form.pwd1.focus(); return false; } if(form.pwd1.value = = form.username.value) { alert("Error: Password must be different from Username!"); form.pwd1.focus(); return false; } re = / [0-9] / ; if re.test(form.pwd1.value)) { alert("Error: password must contain at least one number (0-9)!"); form.pwd1.focus(); return false; } re = / [a-z] / ; if(!re.test(form.pwd1.value)) { alert("Error: password must contain at least one lowercase letter (a-z)!"); form.pwd1.focus(); return false