Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
AnswerRe: Connect to remote SQL Server Pin
EliottA18-Feb-09 11:41
EliottA18-Feb-09 11:41 
GeneralRe: Connect to remote SQL Server Pin
Gindi Bar Yahav18-Feb-09 19:25
Gindi Bar Yahav18-Feb-09 19:25 
GeneralRe: Connect to remote SQL Server Pin
EliottA18-Feb-09 19:29
EliottA18-Feb-09 19:29 
Questiondatabinding + INotifyPropertyChanged Pin
jogisarge18-Feb-09 9:58
jogisarge18-Feb-09 9:58 
AnswerRe: databinding + INotifyPropertyChanged Pin
ABitSmart18-Feb-09 18:22
ABitSmart18-Feb-09 18:22 
QuestionSupport to create graph editor ? [modified] Pin
GuimaSun18-Feb-09 9:24
GuimaSun18-Feb-09 9:24 
AnswerRe: Support to create graph editor ? Pin
Henry Minute18-Feb-09 10:18
Henry Minute18-Feb-09 10:18 
QuestionDynamically create datagrid with prepopulated text boxes Pin
jullesbonnot18-Feb-09 8:20
jullesbonnot18-Feb-09 8:20 
Hi,
I need to build a datagrid dynamically, entirely in c# so no asp tags can be used.

I need my datagrid to contain a textbox in each cell; pre-populated with the corresponding value from the datatable.

My code creates the text box in each cell perfectly BUT every value in the columns is the value of the last cell in that column of the datagrid.


The InstantiateIn method doesn’t seem to be being called for each row and only runs once for the last row. I can’t figure it out....

I pretty inexperienced with c# so any answers would be most appreciated.

Please help.

Thanks,

Jules



//---------------My c# Code Behind file--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_init(object sender, EventArgs e)
{
GenerateDataGrids();
if (!Page.IsPostBack)
{ }
}


public void GenerateDataGrids()
{
//-- Instantiate the data set and table

DataSet ds = new DataSet();

DataTable dt = ds.Tables.Add();

//-- Add columns to the data table

dt.Columns.Add("ID", typeof(int));

dt.Columns.Add("Book", typeof(string));

dt.Columns.Add("Author", typeof(string));

//-- Add rows to the data table

dt.Rows.Add(1, "1984", "George Orwell");

dt.Rows.Add(2, "Notes from the Underground", "Fydor Dostoyevsky");

dt.Rows.Add(3, "The Outsider", "Albert Camus");

dt.Rows.Add(4, "Post Office", "Charles Buchowski");

dt.Rows.Add(5, "The Chant of Maldoror", "Comte De Lautremont");

DataGrid dg = new DataGrid();

dg.ID = ID;
dg.DataSource = ds;
dg.AutoGenerateColumns = false;


DataTable Workdt = new DataTable();
Workdt = ds.Tables[0];


for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
// Creating Template Column
TemplateColumn tc = new TemplateColumn();

string columnName = dt.Columns[i].ColumnName;

tc.HeaderTemplate = new DataGridTemplate(ListItemType.Header, columnName);


for (int j = 1; j <= dt.Rows.Count - 1; j++)
{

string RowCallName = dt.Rows[j][i].ToString();

tc.ItemTemplate = new DataGridTemplate(ListItemType.EditItem, RowCallName);


}
dg.Columns.Add(tc);

}

dg.DataBind();
form1.Controls.Add(dg);
}
}

public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;


public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;

columnName = colname;

}

public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "" + columnName + "";
container.Controls.Add(lc);
break;
case ListItemType.Item:
lc.Text = "Item " + columnName;
container.Controls.Add(lc);
break;
case ListItemType.EditItem:
TextBox tb = new TextBox();
tb.Text = columnName;
container.Controls.Add(tb);
break;
case ListItemType.Footer:
lc.Text = "" + columnName + "";
container.Controls.Add(lc);
break;

}

}



}

]]>

]]>





<title>









AnswerRe: Dynamically create datagrid with prepopulated text boxes Pin
Calin Tatar18-Feb-09 9:14
Calin Tatar18-Feb-09 9:14 
GeneralRe: Dynamically create datagrid with prepopulated text boxes Pin
Calin Tatar18-Feb-09 9:22
Calin Tatar18-Feb-09 9:22 
QuestionJagged array initialization Pin
t_feras18-Feb-09 7:37
t_feras18-Feb-09 7:37 
AnswerRe: Jagged array initialization Pin
harold aptroot18-Feb-09 10:12
harold aptroot18-Feb-09 10:12 
GeneralRe: Jagged array initialization Pin
t_feras18-Feb-09 10:37
t_feras18-Feb-09 10:37 
GeneralRe: Jagged array initialization Pin
harold aptroot18-Feb-09 11:06
harold aptroot18-Feb-09 11:06 
JokeRe: Jagged array initialization Pin
DaveyM6918-Feb-09 11:09
professionalDaveyM6918-Feb-09 11:09 
GeneralRe: Jagged array initialization Pin
harold aptroot18-Feb-09 11:14
harold aptroot18-Feb-09 11:14 
GeneralRe: Jagged array initialization Pin
t_feras18-Feb-09 11:15
t_feras18-Feb-09 11:15 
GeneralRe: Jagged array initialization Pin
harold aptroot18-Feb-09 12:10
harold aptroot18-Feb-09 12:10 
GeneralRe: Jagged array initialization Pin
t_feras18-Feb-09 12:31
t_feras18-Feb-09 12:31 
AnswerRe: Jagged array initialization Pin
Luc Pattyn18-Feb-09 13:10
sitebuilderLuc Pattyn18-Feb-09 13:10 
QuestionDisplaying Ritch textbox contents the same as it is printed Pin
hendrik18-Feb-09 7:29
hendrik18-Feb-09 7:29 
AnswerRe: Displaying Ritch textbox contents the same as it is printed Pin
Dave Kreskowiak18-Feb-09 9:00
mveDave Kreskowiak18-Feb-09 9:00 
QuestionDrop event in RichText Box Pin
Lijo Rajan18-Feb-09 6:37
Lijo Rajan18-Feb-09 6:37 
AnswerRe: Drop event in RichText Box Pin
vaghelabhavesh18-Feb-09 7:32
vaghelabhavesh18-Feb-09 7:32 
QuestionProgramming for synchronization of local database with remote server database. Pin
Digubha18-Feb-09 5:43
Digubha18-Feb-09 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.