How to add a hyperlink to gridview manually - c#

So here is the code and i have the following problem, i don't know how to create a hyperlink object for the column.
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Име на настанот", typeof(string)));
dt.Columns.Add(new DataColumn("Информации за настанот", typeof(string)));
dt.Columns.Add(new DataColumn("Локација", typeof(string)));
dt.Columns.Add(new DataColumn("Време и датум на настанот", typeof(string)));
dt.Columns.Add(new DataColumn("Измени", typeof(HyperLink)));
dt.Columns.Add(new DataColumn("Бриши", typeof(string)));
foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries)
{
HyperLink a = new HyperLink();
a.NavigateUrl = "aaa";
dr = dt.NewRow();
dr["Име на настанот"] = entry.Title.Text.ToString();
dr["Информации за настанот"] = entry.Content.Content.ToString();
dr["Локација"] = entry.Locations[0].ValueString.ToString();
dr["Време и датум на настанот"] = "Почеток: " + entry.Times[0].StartTime.ToString() + " Крај: " + entry.Times[0].EndTime.ToString();
dr["Измени"] = a.NavigateUrl; //what to add here how to add a hyperlink
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
The error i get is:
The XML element 'EnableTheming' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

Make that column a normal string:
dt.Columns.Add(new DataColumn("Измени", typeof(String)));
Then you can simple assign an HTML code for the link:
dr["Измени"] = "<a href='aaa'>Click Here</a>";

You might have to use the RowDataBound event. Or use the <asp:TemplateField> in your grid, this way you can add custom html into a column.

Related

Set More than one Initial row in datatable

The below function set only one Initial row.
How can i make 10 initial rows for example.
Any Suggestions?
Function
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["Sr.No"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
griditem.DataSource = dt;
griditem.DataBind();
}
try this..
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["Sr.No"] = (i +1);
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
griditem.DataSource = dt;
griditem.DataBind();
}
this may be?
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["Sr.No"] = i + 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
You could create your new row in a for loop. Something along the lines of the following:
for (var i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["Sr.No"] = i + 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
This will then give you 10 rows in your dt object, and assign the Sr.No value to the index + 1 of the loop, i.e. 1,2...10
this could also be the option
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn { AutoIncrement=true,AutoIncrementSeed=1,AutoIncrementStep=1,ColumnName="SrNo",DataType=typeof(int)});
dt.Columns.Add(new DataColumn { ColumnName = "Column1", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column2", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column3", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column4", DataType = typeof(string) });
for(int i=0;i<10;i++)
dt.Rows.Add(dt.NewRow());

Bind Rows From GridView to DataTable and Display It In Another GridView RSS

I am going to find a value from a column in a gridview. If there are rows that have this value in that column, I will add those rows to a DataTable. Afterwards I will bind it to another gridview to display those rows out. However, when I tried to do this, columns that are in INT cannot be displayed out in INT but can only be displayed out in TEXT. Also, only 1 row is being binded to the datatable and displayed out in another gridview, I want many rows if many rows have the particular value in that column, not just 1 row.
This is the code I used:
public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("DATE"));
dt.Columns.Add(new DataColumn("CODE"));
dt.Columns.Add(new DataColumn("PERSON_NAME"));
dt.Columns.Add(new DataColumn("STATUS"));
dt.Columns.Add(new DataColumn("HOBBIES"));
dt.Columns.Add(new DataColumn("SCORE"));
dt.Columns.Add(new DataColumn("ITEM"));
dt.Columns.Add(new DataColumn("QUANTITY"));
dt.Columns.Add(new DataColumn("TYPE"));
dt.Columns.Add(new DataColumn("RATING"));
dt.Columns.Add(new DataColumn("PRICE"));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "STATUS")) == "Regular")
{
dr = dt.NewRow();
dr["DATE"] = e.Row.Cells[0].Text;
dr["CODE"] = e.Row.Cells[1].Text;
dr["PERSON_NAME"] = e.Row.Cells[2].Text;
dr["STATUS"] = e.Row.Cells[3].Text;
dr["HOBBIES"] = e.Row.Cells[4].Text;
dr["SCORE"] = e.Row.Cells[5].Text;
dr["ITEM"] = e.Row.Cells[6].Text;
dr["QUANTITY"] = e.Row.Cells[7].Text;
dr["TYPE"] = e.Row.Cells[8].Text;
dr["RATING"] = e.Row.Cells[9].Text;
dr["PRICE"] = e.Row.Cells[10].Text;
dt.Rows.Add(dr);
GridView2.DataSource = dt;
GridView2.DataBind();
}
Can someone please help me on this? Thanks a lot!!
You should use GridView OnDataBound Event instead of RowDataBound. RowDataBound will be triggered after each row is bound to the Gridview where as OnDataBound will be called after all the rows are bound to the Gridview. Try the following
public void GridView1_OnDataBound(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("DATE"));
dt.Columns.Add(new DataColumn("CODE"));
dt.Columns.Add(new DataColumn("PERSON_NAME"));
dt.Columns.Add(new DataColumn("STATUS"));
dt.Columns.Add(new DataColumn("HOBBIES"));
dt.Columns.Add(new DataColumn("SCORE"));
dt.Columns.Add(new DataColumn("ITEM"));
dt.Columns.Add(new DataColumn("QUANTITY"));
dt.Columns.Add(new DataColumn("TYPE"));
dt.Columns.Add(new DataColumn("RATING"));
dt.Columns.Add(new DataColumn("PRICE"));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[3].Text == "Regular")
{
dr = dt.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["CODE"] = gvr.Cells[1].Text;
dr["PERSON_NAME"] = gvr.Cells[2].Text;
dr["STATUS"] = gvr.Cells[3].Text;
dr["HOBBIES"] = gvr.Cells[4].Text;
dr["SCORE"] = gvr.Cells[5].Text;
dr["ITEM"] = gvr.Cells[6].Text;
dr["QUANTITY"] = gvr.Cells[7].Text;
dr["TYPE"] = gvr.Cells[8].Text;
dr["RATING"] = gvr.Cells[9].Text;
dr["PRICE"] = gvr.Cells[10].Text;
dt.Rows.Add(dr);
}
}
GridView2.DataSource = dt;
GridView2.DataBind();
}

Carry Data Table value one place to another place, in windows Application

I have text box control & a button, After putting value in text box & Click on button data should be added in data table.
The problem which i face is, when i added new record, previous record are gone,& new record are come in place of old record.
I come to know, what i use in Windows Application to carry data to one place to another. [viewstate] which we use in WebApplication, But what to do for window application
In this code, it add only one record in the table, when i try to add 2nd record it make replace the old one.
My code are:
public void Getdatatable(int srno, string Name, string address, int contactno,double amount,string Emailid)
{
try
{
dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
dr = dt.NewRow();
dr[0] = srno;
dr[1] = Name;
dr[2] = address;
dr[3] = contactno;
dr[4] = amount;
dr[5] = Emailid;
dt.Rows.Add(dr);
}
catch(Exception ex)
{}
}
private void btnadd_Click(object sender, EventArgs e)
{
srno =Convert.ToInt32(txtsrno.Text);
Name = txtname.Text;
address = txtaddress.Text;
contactno =Convert.ToInt32(txtcontact.Text);
amount =Convert.ToDouble(txtamount.Text);
Emailid = txtemail.Text;
Getdatatable(srno, Name, address, contactno, amount, Emailid);
//dt = ds.Tables[0].Rows[0][0];
foreach (DataRow dr in dt.Rows)
{
dr[0] = txtsrno.Text;
dr[1] = txtname.Text;
dr[2] = txtaddress.Text;
dr[3] = txtcontact.Text;
dr[4] = txtamount.Text;
dr[5] = txtemail.Text;
}
dt.AcceptChanges();
grd.DataSource = dt;
txtsrno.Text = "";
txtname.Text = "";
txtaddress.Text = "";
txtcontact.Text = "";
txtamount.Text = "";
txtemail.Text = "";
}
Well... Your Getdatatable method is creating a new table.
Since you are calling this method in your Click callback, the table is created anew each time. Therefore you only get the last element added.
You should create your table once and remove the Getdatatable(srno, Name, address, contactno, amount, Emailid); call from your button callback.
You could declare DataTable dt as global variable and add columns to it once when form is loaded.
private DataTable dt = null;
private void CreateTable()
{
dt = new DataTable();
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTable();
}
After the CreateTable method was called, you will have data table with columns you want and you don't need to call this method again when you try to add data row to the data table.
Here is the button click event handler
private void ClearTextBox()
{
txtsrno.Text = "";
txtname.Text = "";
txtaddress.Text = "";
txtcontact.Text = "";
txtamount.Text = "";
txtemail.Text = "";
}
private void btnadd_Click(object sender, EventArgs e)
{
try
{
srno = Convert.ToInt32(txtsrno.Text);
Name = txtname.Text;
address = txtaddress.Text;
contactno = Convert.ToInt32(txtcontact.Text);
amount = Convert.ToDouble(txtamount.Text);
Emailid = txtemail.Text;
DataRow dr = dt.NewRow();
dr[0] = srno;
dr[1] = Name;
dr[2] = address;
dr[3] = contactno;
dr[4] = amount;
dr[5] = Emailid;
dt.Rows.Add(dr);
dt.AcceptChanges();
grd.DataSource = dt;
ClearTextBox();
}
catch (Exception ex) { /*Handle Exception*/ }
}
Otherwise, you don't need to have those TextBoxes and button for add data to data table. You just code like this, and enter data in DataGridView cell directly (like when you edit excel file).
private DataTable dt = null;
private void CreateTable()
{
dt = new DataTable();
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
grd.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTable();
}

dynamic gridview column autocalculation

I have a Gridview, with textboxes in templatefields, where i dynamically add columns to it.
protected void grid()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("SerialNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Material", typeof(string)));
dt.Columns.Add(new DataColumn("Bags", typeof(string)));
dt.Columns.Add(new DataColumn("GrossWt", typeof(string)));
dt.Columns.Add(new DataColumn("TareWt", typeof(string)));
dt.Columns.Add(new DataColumn("NetWt", typeof(string)));
dt.Columns.Add(new DataColumn("BillWt", typeof(string)));
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
dr["SerialNumber"] = 1;
GridView1.DataSource = dt;
GridView1.DataBind();
}
I need to have the NetWt column to have autocalulated values using the GrossWt and TareWt columns. The formula should be (grosswt-tarewt)/1000. But i dont know how to go about it. Any ideas??
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[5] = (e.Row.Cells[3].Text - e.Row.Cells[4].Text)/1000
}
}
Try this
Assuming the user edits the values, you can add your own handler to the CellValidated event and then do whatever you like with the values:
myDataGridView.CellValidated += new System.Windows.Forms.DataGridViewCellEventHandler(myDataGridView_CellValidated);
[...]
private void myGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == myGridView.Columns["GrossWt"].Index){
myGridView.Rows[e.Index].Cells["NewWT"].Value = (
myGridView.Rows[e.Index].Cells["GrossWT"].Value -
myGridView.Rows[e.Index].Cells["TareWT"].Value ) /1000;
}
}

How to add gridview rows to a datatable?

I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation...
I have found this http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx
But i want all columns of my gridview to be added to the datarow of the datatable
Grid http://img85.imageshack.us/img85/4044/gridp.jpg
I want to convert gridview to datatable on submit.... Any suggestion...
EDIT:
Answer below works and i have found an answer too...
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
foreach (GridViewRow row in gridEmployee.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DataRow dr = dt.NewRow();
dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
dt.Rows.Add(dr);
}
}
SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
sbc.DestinationTableName = "SalaryDetails";
sbc.ColumnMappings.Add("EmpId", "EmpId");
sbc.ColumnMappings.Add("FromDate", "FromDate");
sbc.ColumnMappings.Add("ToDate", "ToDate");
sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
sbc.WriteToServer(dt);
sbc.Close();
you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.
Another Solution
DataTable dt = new DataTable();
for (int j = 0; j < grdList.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = grdList.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
sbc.DestinationTableName = "yourDestinationTable";
sbc.WriteToServer(dt);
sbc.Close();

Categories